ALSA: asihpi: incorrect range check
[safe/jmp/linux-2.6] / drivers / staging / rar_register / rar_register.c
1 /*
2  *  rar_register.c - An Intel Restricted Access Region register driver
3  *
4  *  Copyright(c) 2009 Intel Corporation. All rights reserved.
5  *
6  *  This program is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU General Public License as
8  *  published by the Free Software Foundation; either version 2 of the
9  *  License, or (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19  *  02111-1307, USA.
20  *
21  * -------------------------------------------------------------------
22  *  20091204 Mark Allyn <mark.a.allyn@intel.com>
23  *           Ossama Othman <ossama.othman@intel.com>
24  *      Cleanup per feedback from Alan Cox and Arjan Van De Ven
25  *
26  *  20090806 Ossama Othman <ossama.othman@intel.com>
27  *      Return zero high address if upper 22 bits is zero.
28  *      Cleaned up checkpatch errors.
29  *      Clarified that driver is dealing with bus addresses.
30  *
31  *  20090702 Ossama Othman <ossama.othman@intel.com>
32  *      Removed unnecessary include directives
33  *      Cleaned up spinlocks.
34  *      Cleaned up logging.
35  *      Improved invalid parameter checks.
36  *      Fixed and simplified RAR address retrieval and RAR locking
37  *      code.
38  *
39  *  20090626 Mark Allyn <mark.a.allyn@intel.com>
40  *      Initial publish
41  */
42
43 #define DEBUG 1
44
45 #include "rar_register.h"
46
47 #include <linux/module.h>
48 #include <linux/pci.h>
49 #include <linux/spinlock.h>
50 #include <linux/device.h>
51 #include <linux/kernel.h>
52
53 /* === Lincroft Message Bus Interface === */
54 /* Message Control Register */
55 #define LNC_MCR_OFFSET 0xD0
56
57 /* Maximum number of clients (other drivers using this driver) */
58 #define MAX_RAR_CLIENTS 10
59
60 /* Message Data Register */
61 #define LNC_MDR_OFFSET 0xD4
62
63 /* Message Opcodes */
64 #define LNC_MESSAGE_READ_OPCODE 0xD0
65 #define LNC_MESSAGE_WRITE_OPCODE 0xE0
66
67 /* Message Write Byte Enables */
68 #define LNC_MESSAGE_BYTE_WRITE_ENABLES 0xF
69
70 /* B-unit Port */
71 #define LNC_BUNIT_PORT 0x3
72
73 /* === Lincroft B-Unit Registers - Programmed by IA32 firmware === */
74 #define LNC_BRAR0L 0x10
75 #define LNC_BRAR0H 0x11
76 #define LNC_BRAR1L 0x12
77 #define LNC_BRAR1H 0x13
78
79 /* Reserved for SeP */
80 #define LNC_BRAR2L 0x14
81 #define LNC_BRAR2H 0x15
82
83 /* Moorestown supports three restricted access regions. */
84 #define MRST_NUM_RAR 3
85
86
87 /* RAR Bus Address Range */
88 struct RAR_address_range {
89         dma_addr_t low;
90         dma_addr_t high;
91 };
92
93 /* Structure containing low and high RAR register offsets. */
94 struct RAR_offsets {
95         u32 low;  /* Register offset for low  RAR bus address. */
96         u32 high; /* Register offset for high RAR bus address. */
97 };
98
99 struct client {
100         int (*client_callback)(void *client_data);
101         void *customer_data;
102         int client_called;
103         };
104
105 static DEFINE_MUTEX(rar_mutex);
106 static DEFINE_MUTEX(lnc_reg_mutex);
107
108 struct RAR_device {
109         struct RAR_offsets const rar_offsets[MRST_NUM_RAR];
110         struct RAR_address_range rar_addr[MRST_NUM_RAR];
111         struct pci_dev *rar_dev;
112         bool registered;
113         };
114
115 /* this platform has only one rar_device for 3 rar regions */
116 static struct RAR_device my_rar_device = {
117         .rar_offsets = {
118                 [0].low = LNC_BRAR0L,
119                 [0].high = LNC_BRAR0H,
120                 [1].low = LNC_BRAR1L,
121                 [1].high = LNC_BRAR1H,
122                 [2].low = LNC_BRAR2L,
123                 [2].high = LNC_BRAR2H
124         }
125 };
126
127 /* this data is for handling requests from other drivers which arrive
128  * prior to this driver initializing
129  */
130
131 static struct client clients[MAX_RAR_CLIENTS];
132 static int num_clients;
133
134 /*
135  * This function is used to retrieved RAR info using the Lincroft
136  * message bus interface.
137  */
138 static int retrieve_rar_addr(struct pci_dev *pdev,
139         int offset,
140         dma_addr_t *addr)
141 {
142         /*
143          * ======== The Lincroft Message Bus Interface ========
144          * Lincroft registers may be obtained from the PCI
145          * (the Host Bridge) using the Lincroft Message Bus
146          * Interface.  That message bus interface is generally
147          * comprised of two registers: a control register (MCR, 0xDO)
148          * and a data register (MDR, 0xD4).
149          *
150          * The MCR (message control register) format is the following:
151          *   1.  [31:24]: Opcode
152          *   2.  [23:16]: Port
153          *   3.  [15:8]: Register Offset
154          *   4.  [7:4]: Byte Enables (use 0xF to set all of these bits
155          *              to 1)
156          *   5.  [3:0]: reserved
157          *
158          *  Read (0xD0) and write (0xE0) opcodes are written to the
159          *  control register when reading and writing to Lincroft
160          *  registers, respectively.
161          *
162          *  We're interested in registers found in the Lincroft
163          *  B-unit.  The B-unit port is 0x3.
164          *
165          *  The six B-unit RAR register offsets we use are listed
166          *  earlier in this file.
167          *
168          *  Lastly writing to the MCR register requires the "Byte
169          *  enables" bits to be set to 1.  This may be achieved by
170          *  writing 0xF at bit 4.
171          *
172          * The MDR (message data register) format is the following:
173          *   1. [31:0]: Read/Write Data
174          *
175          *  Data being read from this register is only available after
176          *  writing the appropriate control message to the MCR
177          *  register.
178          *
179          *  Data being written to this register must be written before
180          *  writing the appropriate control message to the MCR
181          *  register.
182         */
183
184         int result;
185
186         /* Construct control message */
187         u32 const message =
188                  (LNC_MESSAGE_READ_OPCODE << 24)
189                  | (LNC_BUNIT_PORT << 16)
190                  | (offset << 8)
191                  | (LNC_MESSAGE_BYTE_WRITE_ENABLES << 4);
192
193         dev_dbg(&pdev->dev, "Offset for 'get' LNC MSG is %x\n", offset);
194
195         if (addr == 0) {
196                 WARN_ON(1);
197                 return -EINVAL;
198         }
199
200         /*
201         * We synchronize access to the Lincroft MCR and MDR registers
202         * until BOTH the command is issued through the MCR register
203         * and the corresponding data is read from the MDR register.
204         * Otherwise a race condition would exist between accesses to
205         * both registers.
206         */
207
208         mutex_lock(&lnc_reg_mutex);
209
210         /* Send the control message */
211         result = pci_write_config_dword(pdev, LNC_MCR_OFFSET, message);
212
213         dev_dbg(&pdev->dev, "Result from send ctl register is %x\n", result);
214
215         if (!result) {
216                 result = pci_read_config_dword(pdev, LNC_MDR_OFFSET,
217                         (u32 *)addr);
218                 dev_dbg(&pdev->dev,
219                         "Result from read data register is %x\n", result);
220
221                 dev_dbg(&pdev->dev,
222                         "Value read from data register is %lx\n",
223                          (unsigned long)*addr);
224         }
225
226         mutex_unlock(&lnc_reg_mutex);
227
228         return result;
229 }
230
231 static int set_rar_address(struct pci_dev *pdev,
232         int offset,
233         dma_addr_t addr)
234 {
235         /*
236         * Data being written to this register must be written before
237         * writing the appropriate control message to the MCR
238         * register.
239         * @note See rar_get_address() for a description of the
240         * message bus interface being used here.
241         */
242
243         int result = 0;
244
245         /* Construct control message */
246         u32 const message = (LNC_MESSAGE_WRITE_OPCODE << 24)
247                 | (LNC_BUNIT_PORT << 16)
248                 | (offset << 8)
249                 | (LNC_MESSAGE_BYTE_WRITE_ENABLES << 4);
250
251         if (addr == 0) {
252                 WARN_ON(1);
253                 return -EINVAL;
254         }
255
256         dev_dbg(&pdev->dev, "Offset for 'set' LNC MSG is %x\n", offset);
257
258         /*
259         * We synchronize access to the Lincroft MCR and MDR registers
260         * until BOTH the command is issued through the MCR register
261         * and the corresponding data is read from the MDR register.
262         * Otherwise a race condition would exist between accesses to
263         * both registers.
264         */
265
266         mutex_lock(&lnc_reg_mutex);
267
268         /* Send the control message */
269         result = pci_write_config_dword(pdev, LNC_MDR_OFFSET, addr);
270
271         dev_dbg(&pdev->dev, "Result from write data register is %x\n", result);
272
273         if (!result) {
274                 dev_dbg(&pdev->dev,
275                         "Value written to data register is %lx\n",
276                          (unsigned long)addr);
277
278                 result = pci_write_config_dword(pdev, LNC_MCR_OFFSET, message);
279
280                 dev_dbg(&pdev->dev, "Result from send ctl register is %x\n",
281                         result);
282         }
283
284         mutex_unlock(&lnc_reg_mutex);
285
286         return result;
287 }
288
289 /*
290 * Initialize RAR parameters, such as bus addresses, etc.
291 */
292 static int init_rar_params(struct pci_dev *pdev)
293 {
294         unsigned int i;
295         int result = 0;
296
297         /* Retrieve RAR start and end bus addresses.
298         * Access the RAR registers through the Lincroft Message Bus
299         * Interface on PCI device: 00:00.0 Host bridge.
300         */
301
302         for (i = 0; i < MRST_NUM_RAR; ++i) {
303                 struct RAR_offsets const *offset =
304                         &my_rar_device.rar_offsets[i];
305                 struct RAR_address_range *addr = &my_rar_device.rar_addr[i];
306
307         if ((retrieve_rar_addr(pdev, offset->low, &addr->low) != 0)
308                 || (retrieve_rar_addr(pdev, offset->high, &addr->high) != 0)) {
309                 result = -1;
310                 break;
311                 }
312
313                 /*
314                 * Only the upper 22 bits of the RAR addresses are
315                 * stored in their corresponding RAR registers so we
316                 * must set the lower 10 bits accordingly.
317
318                 * The low address has its lower 10 bits cleared, and
319                 * the high address has all its lower 10 bits set,
320                 * e.g.:
321                 * low = 0x2ffffc00
322                 */
323
324                 addr->low &= (dma_addr_t)0xfffffc00u;
325
326                 /*
327                 * Set bits 9:0 on uppser address if bits 31:10 are non
328                 * zero; otherwize clear all bits
329                 */
330
331                 if ((addr->high & 0xfffffc00u) == 0)
332                         addr->high = 0;
333                 else
334                         addr->high |= 0x3ffu;
335         }
336         /* Done accessing the device. */
337
338         if (result == 0) {
339                 int z;
340                 for (z = 0; z != MRST_NUM_RAR; ++z) {
341                         /*
342                         * "BRAR" refers to the RAR registers in the
343                         * Lincroft B-unit.
344                         */
345                         dev_info(&pdev->dev, "BRAR[%u] bus address range = "
346                           "[%lx, %lx]\n", z,
347                           (unsigned long)my_rar_device.rar_addr[z].low,
348                           (unsigned long)my_rar_device.rar_addr[z].high);
349                 }
350         }
351
352         return result;
353 }
354
355 /*
356  * The rar_get_address function is used by other device drivers
357  * to obtain RAR address information on a RAR. It takes three
358  * parameters:
359  *
360  * int rar_index
361  * The rar_index is an index to the rar for which you wish to retrieve
362  * the address information.
363  * Values can be 0,1, or 2.
364  *
365  * The function returns a 0 upon success or a -1 if there is no RAR
366  * facility on this system.
367  */
368 int rar_get_address(int rar_index,
369         dma_addr_t *start_address,
370         dma_addr_t *end_address)
371 {
372         int result = -ENODEV;
373
374         if (my_rar_device.registered) {
375                 if (start_address == 0 || end_address == 0
376                         || rar_index >= MRST_NUM_RAR || rar_index < 0) {
377                         result = -EINVAL;
378                 } else {
379                         *start_address =
380                                 my_rar_device.rar_addr[rar_index].low;
381                         *end_address =
382                                 my_rar_device.rar_addr[rar_index].high;
383
384                         result = 0;
385                 }
386         }
387
388         return result;
389 }
390 EXPORT_SYMBOL(rar_get_address);
391
392 /*
393  * The rar_lock function is ued by other device drivers to lock an RAR.
394  * once an RAR is locked, it stays locked until the next system reboot.
395  * The function takes one parameter:
396  *
397  * int rar_index
398  * The rar_index is an index to the rar that you want to lock.
399  * Values can be 0,1, or 2.
400  *
401  * The function returns a 0 upon success or a -1 if there is no RAR
402  * facility on this system.
403  */
404 int rar_lock(int rar_index)
405 {
406         int result = -ENODEV;
407
408         if (rar_index >= MRST_NUM_RAR || rar_index < 0) {
409                 result = -EINVAL;
410                 return result;
411         }
412
413         dev_dbg(&my_rar_device.rar_dev->dev, "rar_lock mutex locking\n");
414         mutex_lock(&rar_mutex);
415
416         if (my_rar_device.registered) {
417
418                 dma_addr_t low = my_rar_device.rar_addr[rar_index].low &
419                         0xfffffc00u;
420
421                 dma_addr_t high = my_rar_device.rar_addr[rar_index].high &
422                         0xfffffc00u;
423
424                 /*
425                 * Only allow I/O from the graphics and Langwell;
426                 * Not from the x96 processor
427                 */
428                 if (rar_index == (int)RAR_TYPE_VIDEO) {
429                         low |= 0x00000009;
430                         high |= 0x00000015;
431                 }
432
433                 else if (rar_index == (int)RAR_TYPE_AUDIO) {
434                         /* Only allow I/O from Langwell; nothing from x86 */
435                         low |= 0x00000008;
436                         high |= 0x00000018;
437                 }
438
439                 else
440                         /* Read-only from all agents */
441                         high |= 0x00000018;
442
443                 /*
444                 * Now program the register using the Lincroft message
445                 * bus interface.
446                 */
447                 result = set_rar_address(my_rar_device.rar_dev,
448                         my_rar_device.rar_offsets[rar_index].low,
449                         low);
450
451                 if (result == 0)
452                         result = set_rar_address(
453                         my_rar_device.rar_dev,
454                         my_rar_device.rar_offsets[rar_index].high,
455                         high);
456         }
457
458         dev_dbg(&my_rar_device.rar_dev->dev, "rar_lock mutex unlocking\n");
459         mutex_unlock(&rar_mutex);
460         return result;
461 }
462 EXPORT_SYMBOL(rar_lock);
463
464 /* The register_rar function is to used by other device drivers
465  * to ensure that this driver is ready. As we cannot be sure of
466  * the compile/execute order of dirvers in ther kernel, it is
467  * best to give this driver a callback function to call when
468  * it is ready to give out addresses. The callback function
469  * would have those steps that continue the initialization of
470  * a driver that do require a valid RAR address. One of those
471  * steps would be to call rar_get_address()
472  * This function return 0 on success an -1 on failure.
473 */
474 int register_rar(int (*callback)(void *yourparameter), void *yourparameter)
475 {
476
477         int result = -ENODEV;
478
479         if (callback == NULL)
480                 return -EINVAL;
481
482         mutex_lock(&rar_mutex);
483
484         if (my_rar_device.registered) {
485
486                 mutex_unlock(&rar_mutex);
487                 /*
488                 * if the driver already registered, then we can simply
489                 * call the callback right now
490                 */
491
492                 return (*callback)(yourparameter);
493         }
494
495         if (num_clients < MRST_NUM_RAR) {
496
497                 clients[num_clients].client_callback = callback;
498                 clients[num_clients].customer_data = yourparameter;
499                 num_clients += 1;
500                 result = 0;
501         }
502
503         mutex_unlock(&rar_mutex);
504         return result;
505
506 }
507 EXPORT_SYMBOL(register_rar);
508
509 /* Suspend - returns -ENOSYS */
510 static int rar_suspend(struct pci_dev *dev, pm_message_t state)
511 {
512         return -ENOSYS;
513 }
514
515 static int rar_resume(struct pci_dev *dev)
516 {
517         return -ENOSYS;
518 }
519
520 /*
521  * This function registers the driver with the device subsystem (
522  * either PCI, USB, etc).
523  * Function that is activaed on the succesful probe of the RAR device
524  * (Moorestown host controller).
525  */
526 static int rar_probe(struct pci_dev *dev, const struct pci_device_id *id)
527 {
528         int error;
529         int counter;
530
531         dev_dbg(&dev->dev, "PCI probe starting\n");
532
533         /* enable the device */
534         error = pci_enable_device(dev);
535         if (error) {
536                 dev_err(&dev->dev,
537                         "Error enabling RAR register PCI device\n");
538                 goto end_function;
539         }
540
541         /* we have only one device; fill in the rar_device structure */
542         my_rar_device.rar_dev = dev;
543
544         /*
545         * Initialize the RAR parameters, which have to be retrieved
546         * via the message bus interface.
547         */
548         error = init_rar_params(dev);
549         if (error) {
550                 pci_disable_device(dev);
551
552                 dev_err(&dev->dev,
553                         "Error retrieving RAR addresses\n");
554
555                 goto end_function;
556         }
557
558         dev_dbg(&dev->dev, "PCI probe locking\n");
559         mutex_lock(&rar_mutex);
560         my_rar_device.registered = 1;
561
562         /* now call anyone who has registered (using callbacks) */
563         for (counter = 0; counter < num_clients; counter += 1) {
564                 if (clients[counter].client_callback) {
565                         error = (*clients[counter].client_callback)(
566                                 clients[counter].customer_data);
567                         /* set callback to NULL to indicate it has been done */
568                         clients[counter].client_callback = NULL;
569                                 dev_dbg(&my_rar_device.rar_dev->dev,
570                                 "Callback called for %d\n",
571                         counter);
572                 }
573         }
574
575         dev_dbg(&dev->dev, "PCI probe unlocking\n");
576         mutex_unlock(&rar_mutex);
577
578 end_function:
579
580         return error;
581 }
582
583 const struct pci_device_id rar_pci_id_tbl[] = {
584         { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_RAR_DEVICE_ID) },
585         { 0 }
586 };
587
588 MODULE_DEVICE_TABLE(pci, rar_pci_id_tbl);
589
590 const struct pci_device_id *my_id_table = rar_pci_id_tbl;
591
592 /* field for registering driver to PCI device */
593 static struct pci_driver rar_pci_driver = {
594         .name = "rar_register_driver",
595         .id_table = rar_pci_id_tbl,
596         .probe = rar_probe,
597         .suspend = rar_suspend,
598         .resume = rar_resume
599 };
600
601 static int __init rar_init_handler(void)
602 {
603         return pci_register_driver(&rar_pci_driver);
604 }
605
606 static void __exit rar_exit_handler(void)
607 {
608         pci_unregister_driver(&rar_pci_driver);
609 }
610
611 module_init(rar_init_handler);
612 module_exit(rar_exit_handler);
613
614 MODULE_LICENSE("GPL");
615 MODULE_DESCRIPTION("Intel Restricted Access Region Register Driver");