2 * rar_register.c - An Intel Restricted Access Region register driver
4 * Copyright(c) 2009 Intel Corporation. All rights reserved.
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.
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.
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
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
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.
31 * 20090702 Ossama Othman <ossama.othman@intel.com>
32 * Removed unnecessary include directives
33 * Cleaned up spinlocks.
35 * Improved invalid parameter checks.
36 * Fixed and simplified RAR address retrieval and RAR locking
39 * 20090626 Mark Allyn <mark.a.allyn@intel.com>
45 #include "rar_register.h"
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>
53 /* === Lincroft Message Bus Interface === */
54 /* Message Control Register */
55 #define LNC_MCR_OFFSET 0xD0
57 /* Maximum number of clients (other drivers using this driver) */
58 #define MAX_RAR_CLIENTS 10
60 /* Message Data Register */
61 #define LNC_MDR_OFFSET 0xD4
64 #define LNC_MESSAGE_READ_OPCODE 0xD0
65 #define LNC_MESSAGE_WRITE_OPCODE 0xE0
67 /* Message Write Byte Enables */
68 #define LNC_MESSAGE_BYTE_WRITE_ENABLES 0xF
71 #define LNC_BUNIT_PORT 0x3
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
79 /* Reserved for SeP */
80 #define LNC_BRAR2L 0x14
81 #define LNC_BRAR2H 0x15
83 /* Moorestown supports three restricted access regions. */
84 #define MRST_NUM_RAR 3
87 /* RAR Bus Address Range */
88 struct RAR_address_range {
93 /* Structure containing low and high RAR register offsets. */
95 u32 low; /* Register offset for low RAR bus address. */
96 u32 high; /* Register offset for high RAR bus address. */
100 int (*client_callback)(void *client_data);
105 static DEFINE_MUTEX(rar_mutex);
106 static DEFINE_MUTEX(lnc_reg_mutex);
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;
115 /* this platform has only one rar_device for 3 rar regions */
116 static struct RAR_device my_rar_device = {
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
127 /* this data is for handling requests from other drivers which arrive
128 * prior to this driver initializing
131 static struct client clients[MAX_RAR_CLIENTS];
132 static int num_clients;
135 * This function is used to retrieved RAR info using the Lincroft
136 * message bus interface.
138 static int retrieve_rar_addr(struct pci_dev *pdev,
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).
150 * The MCR (message control register) format is the following:
153 * 3. [15:8]: Register Offset
154 * 4. [7:4]: Byte Enables (use 0xF to set all of these bits
158 * Read (0xD0) and write (0xE0) opcodes are written to the
159 * control register when reading and writing to Lincroft
160 * registers, respectively.
162 * We're interested in registers found in the Lincroft
163 * B-unit. The B-unit port is 0x3.
165 * The six B-unit RAR register offsets we use are listed
166 * earlier in this file.
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.
172 * The MDR (message data register) format is the following:
173 * 1. [31:0]: Read/Write Data
175 * Data being read from this register is only available after
176 * writing the appropriate control message to the MCR
179 * Data being written to this register must be written before
180 * writing the appropriate control message to the MCR
186 /* Construct control message */
188 (LNC_MESSAGE_READ_OPCODE << 24)
189 | (LNC_BUNIT_PORT << 16)
191 | (LNC_MESSAGE_BYTE_WRITE_ENABLES << 4);
193 dev_dbg(&pdev->dev, "Offset for 'get' LNC MSG is %x\n", offset);
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
208 mutex_lock(&lnc_reg_mutex);
210 /* Send the control message */
211 result = pci_write_config_dword(pdev, LNC_MCR_OFFSET, message);
213 dev_dbg(&pdev->dev, "Result from send ctl register is %x\n", result);
216 result = pci_read_config_dword(pdev, LNC_MDR_OFFSET,
219 "Result from read data register is %x\n", result);
222 "Value read from data register is %lx\n",
223 (unsigned long)*addr);
226 mutex_unlock(&lnc_reg_mutex);
231 static int set_rar_address(struct pci_dev *pdev,
236 * Data being written to this register must be written before
237 * writing the appropriate control message to the MCR
239 * @note See rar_get_address() for a description of the
240 * message bus interface being used here.
245 /* Construct control message */
246 u32 const message = (LNC_MESSAGE_WRITE_OPCODE << 24)
247 | (LNC_BUNIT_PORT << 16)
249 | (LNC_MESSAGE_BYTE_WRITE_ENABLES << 4);
256 dev_dbg(&pdev->dev, "Offset for 'set' LNC MSG is %x\n", offset);
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
266 mutex_lock(&lnc_reg_mutex);
268 /* Send the control message */
269 result = pci_write_config_dword(pdev, LNC_MDR_OFFSET, addr);
271 dev_dbg(&pdev->dev, "Result from write data register is %x\n", result);
275 "Value written to data register is %lx\n",
276 (unsigned long)addr);
278 result = pci_write_config_dword(pdev, LNC_MCR_OFFSET, message);
280 dev_dbg(&pdev->dev, "Result from send ctl register is %x\n",
284 mutex_unlock(&lnc_reg_mutex);
290 * Initialize RAR parameters, such as bus addresses, etc.
292 static int init_rar_params(struct pci_dev *pdev)
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.
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];
307 if ((retrieve_rar_addr(pdev, offset->low, &addr->low) != 0)
308 || (retrieve_rar_addr(pdev, offset->high, &addr->high) != 0)) {
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.
318 * The low address has its lower 10 bits cleared, and
319 * the high address has all its lower 10 bits set,
324 addr->low &= (dma_addr_t)0xfffffc00u;
327 * Set bits 9:0 on uppser address if bits 31:10 are non
328 * zero; otherwize clear all bits
331 if ((addr->high & 0xfffffc00u) == 0)
334 addr->high |= 0x3ffu;
336 /* Done accessing the device. */
340 for (z = 0; z != MRST_NUM_RAR; ++z) {
342 * "BRAR" refers to the RAR registers in the
345 dev_info(&pdev->dev, "BRAR[%u] bus address range = "
347 (unsigned long)my_rar_device.rar_addr[z].low,
348 (unsigned long)my_rar_device.rar_addr[z].high);
356 * The rar_get_address function is used by other device drivers
357 * to obtain RAR address information on a RAR. It takes three
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.
365 * The function returns a 0 upon success or a -1 if there is no RAR
366 * facility on this system.
368 int rar_get_address(int rar_index,
369 dma_addr_t *start_address,
370 dma_addr_t *end_address)
372 int result = -ENODEV;
374 if (my_rar_device.registered) {
375 if (start_address == 0 || end_address == 0
376 || rar_index >= MRST_NUM_RAR || rar_index < 0) {
380 my_rar_device.rar_addr[rar_index].low;
382 my_rar_device.rar_addr[rar_index].high;
390 EXPORT_SYMBOL(rar_get_address);
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:
398 * The rar_index is an index to the rar that you want to lock.
399 * Values can be 0,1, or 2.
401 * The function returns a 0 upon success or a -1 if there is no RAR
402 * facility on this system.
404 int rar_lock(int rar_index)
406 int result = -ENODEV;
408 if (rar_index >= MRST_NUM_RAR || rar_index < 0) {
413 dev_dbg(&my_rar_device.rar_dev->dev, "rar_lock mutex locking\n");
414 mutex_lock(&rar_mutex);
416 if (my_rar_device.registered) {
418 dma_addr_t low = my_rar_device.rar_addr[rar_index].low &
421 dma_addr_t high = my_rar_device.rar_addr[rar_index].high &
425 * Only allow I/O from the graphics and Langwell;
426 * Not from the x96 processor
428 if (rar_index == (int)RAR_TYPE_VIDEO) {
433 else if (rar_index == (int)RAR_TYPE_AUDIO) {
434 /* Only allow I/O from Langwell; nothing from x86 */
440 /* Read-only from all agents */
444 * Now program the register using the Lincroft message
447 result = set_rar_address(my_rar_device.rar_dev,
448 my_rar_device.rar_offsets[rar_index].low,
452 result = set_rar_address(
453 my_rar_device.rar_dev,
454 my_rar_device.rar_offsets[rar_index].high,
458 dev_dbg(&my_rar_device.rar_dev->dev, "rar_lock mutex unlocking\n");
459 mutex_unlock(&rar_mutex);
462 EXPORT_SYMBOL(rar_lock);
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.
474 int register_rar(int (*callback)(void *yourparameter), void *yourparameter)
477 int result = -ENODEV;
479 if (callback == NULL)
482 mutex_lock(&rar_mutex);
484 if (my_rar_device.registered) {
486 mutex_unlock(&rar_mutex);
488 * if the driver already registered, then we can simply
489 * call the callback right now
492 return (*callback)(yourparameter);
495 if (num_clients < MRST_NUM_RAR) {
497 clients[num_clients].client_callback = callback;
498 clients[num_clients].customer_data = yourparameter;
503 mutex_unlock(&rar_mutex);
507 EXPORT_SYMBOL(register_rar);
510 * This function registers the driver with the device subsystem (
511 * either PCI, USB, etc).
512 * Function that is activaed on the succesful probe of the RAR device
513 * (Moorestown host controller).
515 static int rar_probe(struct pci_dev *dev, const struct pci_device_id *id)
520 dev_dbg(&dev->dev, "PCI probe starting\n");
522 /* enable the device */
523 error = pci_enable_device(dev);
526 "Error enabling RAR register PCI device\n");
530 /* we have only one device; fill in the rar_device structure */
531 my_rar_device.rar_dev = dev;
534 * Initialize the RAR parameters, which have to be retrieved
535 * via the message bus interface.
537 error = init_rar_params(dev);
539 pci_disable_device(dev);
542 "Error retrieving RAR addresses\n");
547 dev_dbg(&dev->dev, "PCI probe locking\n");
548 mutex_lock(&rar_mutex);
549 my_rar_device.registered = 1;
551 /* now call anyone who has registered (using callbacks) */
552 for (counter = 0; counter < num_clients; counter += 1) {
553 if (clients[counter].client_callback) {
554 error = (*clients[counter].client_callback)(
555 clients[counter].customer_data);
556 /* set callback to NULL to indicate it has been done */
557 clients[counter].client_callback = NULL;
558 dev_dbg(&my_rar_device.rar_dev->dev,
559 "Callback called for %d\n",
564 dev_dbg(&dev->dev, "PCI probe unlocking\n");
565 mutex_unlock(&rar_mutex);
572 const struct pci_device_id rar_pci_id_tbl[] = {
573 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_RAR_DEVICE_ID) },
577 MODULE_DEVICE_TABLE(pci, rar_pci_id_tbl);
579 const struct pci_device_id *my_id_table = rar_pci_id_tbl;
581 /* field for registering driver to PCI device */
582 static struct pci_driver rar_pci_driver = {
583 .name = "rar_register_driver",
584 .id_table = rar_pci_id_tbl,
588 static int __init rar_init_handler(void)
590 return pci_register_driver(&rar_pci_driver);
593 static void __exit rar_exit_handler(void)
595 pci_unregister_driver(&rar_pci_driver);
598 module_init(rar_init_handler);
599 module_exit(rar_exit_handler);
601 MODULE_LICENSE("GPL");
602 MODULE_DESCRIPTION("Intel Restricted Access Region Register Driver");