Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Staging: rar_register: fix checkpatch errors and debug program file

Signed-off-by: Mark Allyn <mark.a.allyn@intel.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by

Mark Allyn and committed by
Greg Kroah-Hartman
433e63c6 9cedb392

+518 -356
+518 -356
drivers/staging/rar_register/rar_register.c
··· 1 - #include <linux/init.h> 2 - #include <linux/module.h> 3 - #include <linux/fs.h> 4 - #include <linux/cdev.h> 5 - #include <linux/kdev_t.h> 6 - #include <linux/semaphore.h> 7 - #include <linux/mm.h> 8 - #include <linux/poll.h> 9 - #include <linux/wait.h> 10 - #include <linux/ioctl.h> 11 - #include <linux/ioport.h> 12 - #include <linux/io.h> 13 - #include <linux/interrupt.h> 14 - #include <linux/pagemap.h> 15 - #include <linux/pci.h> 16 - #include <linux/firmware.h> 17 - #include <linux/sched.h> 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 + 18 45 #include "rar_register.h" 19 46 20 - /* The following defines are for the IPC process to retrieve RAR in */ 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> 21 52 22 53 /* === Lincroft Message Bus Interface === */ 23 54 /* Message Control Register */ 24 55 #define LNC_MCR_OFFSET 0xD0 25 56 57 + /* Maximum number of clients (other drivers using this driver) */ 58 + #define MAX_RAR_CLIENTS 10 59 + 26 60 /* Message Data Register */ 27 61 #define LNC_MDR_OFFSET 0xD4 28 62 29 63 /* Message Opcodes */ 30 - #define LNC_MESSAGE_READ_OPCODE 0xD0 64 + #define LNC_MESSAGE_READ_OPCODE 0xD0 31 65 #define LNC_MESSAGE_WRITE_OPCODE 0xE0 32 66 33 67 /* Message Write Byte Enables */ ··· 71 37 #define LNC_BUNIT_PORT 0x3 72 38 73 39 /* === 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 40 + #define LNC_BRAR0L 0x10 41 + #define LNC_BRAR0H 0x11 42 + #define LNC_BRAR1L 0x12 43 + #define LNC_BRAR1H 0x13 78 44 79 45 /* Reserved for SeP */ 80 - #define LNC_BRAR2L 0x14 81 - #define LNC_BRAR2H 0x15 82 - 83 - 84 - /* This structure is only used during module initialization. */ 85 - struct RAR_offsets { 86 - int low; /* Register offset for low RAR physical address. */ 87 - int high; /* Register offset for high RAR physical address. */ 88 - }; 89 - 90 - struct pci_dev *rar_dev; 91 - static uint32_t registered; 46 + #define LNC_BRAR2L 0x14 47 + #define LNC_BRAR2H 0x15 92 48 93 49 /* Moorestown supports three restricted access regions. */ 94 50 #define MRST_NUM_RAR 3 95 51 96 - struct RAR_address_struct rar_addr[MRST_NUM_RAR]; 97 52 98 - /* prototype for init */ 99 - static int __init rar_init_handler(void); 100 - static void __exit rar_exit_handler(void); 101 - 102 - /* 103 - function that is activated on the successfull probe of the RAR device 104 - */ 105 - static int __devinit rar_probe(struct pci_dev *pdev, 106 - const struct pci_device_id *ent); 107 - 108 - static const struct pci_device_id rar_pci_id_tbl[] = { 109 - { PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x4110) }, 110 - { 0 } 53 + /* RAR Bus Address Range */ 54 + struct RAR_address_range { 55 + dma_addr_t low; 56 + dma_addr_t high; 111 57 }; 112 58 113 - MODULE_DEVICE_TABLE(pci, rar_pci_id_tbl); 114 - 115 - /* field for registering driver to PCI device */ 116 - static struct pci_driver rar_pci_driver = { 117 - .name = "rar_driver", 118 - .id_table = rar_pci_id_tbl, 119 - .probe = rar_probe 59 + /* Structure containing low and high RAR register offsets. */ 60 + struct RAR_offsets { 61 + u32 low; /* Register offset for low RAR bus address. */ 62 + u32 high; /* Register offset for high RAR bus address. */ 120 63 }; 121 64 122 - /* This function is used to retrieved RAR info using the IPC message 123 - bus interface */ 124 - static int memrar_get_rar_addr(struct pci_dev *pdev, 125 - int offset, 126 - u32 *addr) 127 - { 128 - /* 129 - * ======== The Lincroft Message Bus Interface ======== 130 - * Lincroft registers may be obtained from the PCI 131 - * (the Host Bridge) using the Lincroft Message Bus 132 - * Interface. That message bus interface is generally 133 - * comprised of two registers: a control register (MCR, 0xDO) 134 - * and a data register (MDR, 0xD4). 135 - * 136 - * The MCR (message control register) format is the following: 137 - * 1. [31:24]: Opcode 138 - * 2. [23:16]: Port 139 - * 3. [15:8]: Register Offset 140 - * 4. [7:4]: Byte Enables (use 0xF to set all of these bits 141 - * to 1) 142 - * 5. [3:0]: reserved 143 - * 144 - * Read (0xD0) and write (0xE0) opcodes are written to the 145 - * control register when reading and writing to Lincroft 146 - * registers, respectively. 147 - * 148 - * We're interested in registers found in the Lincroft 149 - * B-unit. The B-unit port is 0x3. 150 - * 151 - * The six B-unit RAR register offsets we use are listed 152 - * earlier in this file. 153 - * 154 - * Lastly writing to the MCR register requires the "Byte 155 - * enables" bits to be set to 1. This may be achieved by 156 - * writing 0xF at bit 4. 157 - * 158 - * The MDR (message data register) format is the following: 159 - * 1. [31:0]: Read/Write Data 160 - * 161 - * Data being read from this register is only available after 162 - * writing the appropriate control message to the MCR 163 - * register. 164 - * 165 - * Data being written to this register must be written before 166 - * writing the appropriate control message to the MCR 167 - * register. 168 - */ 169 - 170 - int result = 0; /* result */ 171 - /* Construct control message */ 172 - u32 const message = 173 - (LNC_MESSAGE_READ_OPCODE << 24) 174 - | (LNC_BUNIT_PORT << 16) 175 - | (offset << 8) 176 - | (LNC_MESSAGE_BYTE_WRITE_ENABLES << 4); 177 - 178 - printk(KERN_WARNING "rar- offset to LNC MSG is %x\n", offset); 179 - 180 - if (addr == 0) 181 - return -EINVAL; 182 - 183 - /* Send the control message */ 184 - result = pci_write_config_dword(pdev, 185 - LNC_MCR_OFFSET, 186 - message); 187 - 188 - printk(KERN_WARNING "rar- result from send ctl register is %x\n", 189 - result); 190 - 191 - if (!result) 192 - result = pci_read_config_dword(pdev, 193 - LNC_MDR_OFFSET, 194 - addr); 195 - 196 - printk(KERN_WARNING "rar- result from read data register is %x\n", 197 - result); 198 - 199 - printk(KERN_WARNING "rar- value read from data register is %x\n", 200 - *addr); 201 - 202 - if (result) 203 - return -1; 204 - else 205 - return 0; 206 - } 207 - 208 - static int memrar_set_rar_addr(struct pci_dev *pdev, 209 - int offset, 210 - u32 addr) 211 - { 212 - /* 213 - * ======== The Lincroft Message Bus Interface ======== 214 - * Lincroft registers may be obtained from the PCI 215 - * (the Host Bridge) using the Lincroft Message Bus 216 - * Interface. That message bus interface is generally 217 - * comprised of two registers: a control register (MCR, 0xDO) 218 - * and a data register (MDR, 0xD4). 219 - * 220 - * The MCR (message control register) format is the following: 221 - * 1. [31:24]: Opcode 222 - * 2. [23:16]: Port 223 - * 3. [15:8]: Register Offset 224 - * 4. [7:4]: Byte Enables (use 0xF to set all of these bits 225 - * to 1) 226 - * 5. [3:0]: reserved 227 - * 228 - * Read (0xD0) and write (0xE0) opcodes are written to the 229 - * control register when reading and writing to Lincroft 230 - * registers, respectively. 231 - * 232 - * We're interested in registers found in the Lincroft 233 - * B-unit. The B-unit port is 0x3. 234 - * 235 - * The six B-unit RAR register offsets we use are listed 236 - * earlier in this file. 237 - * 238 - * Lastly writing to the MCR register requires the "Byte 239 - * enables" bits to be set to 1. This may be achieved by 240 - * writing 0xF at bit 4. 241 - * 242 - * The MDR (message data register) format is the following: 243 - * 1. [31:0]: Read/Write Data 244 - * 245 - * Data being read from this register is only available after 246 - * writing the appropriate control message to the MCR 247 - * register. 248 - * 249 - * Data being written to this register must be written before 250 - * writing the appropriate control message to the MCR 251 - * register. 252 - */ 253 - 254 - int result = 0; /* result */ 255 - 256 - /* Construct control message */ 257 - u32 const message = 258 - (LNC_MESSAGE_WRITE_OPCODE << 24) 259 - | (LNC_BUNIT_PORT << 16) 260 - | (offset << 8) 261 - | (LNC_MESSAGE_BYTE_WRITE_ENABLES << 4); 262 - 263 - printk(KERN_WARNING "rar- offset to LNC MSG is %x\n", offset); 264 - 265 - if (addr == 0) 266 - return -EINVAL; 267 - 268 - /* Send the control message */ 269 - result = pci_write_config_dword(pdev, 270 - LNC_MDR_OFFSET, 271 - addr); 272 - 273 - printk(KERN_WARNING "rar- result from send ctl register is %x\n", 274 - result); 275 - 276 - if (!result) 277 - result = pci_write_config_dword(pdev, 278 - LNC_MCR_OFFSET, 279 - message); 280 - 281 - printk(KERN_WARNING "rar- result from write data register is %x\n", 282 - result); 283 - 284 - printk(KERN_WARNING "rar- value read to data register is %x\n", 285 - addr); 286 - 287 - if (result) 288 - return -1; 289 - else 290 - return 0; 291 - } 292 - 293 - /* 294 - 295 - * Initialize RAR parameters, such as physical addresses, etc. 296 - 297 - */ 298 - static int memrar_init_rar_params(struct pci_dev *pdev) 299 - { 300 - struct RAR_offsets const offsets[] = { 301 - { LNC_BRAR0L, LNC_BRAR0H }, 302 - { LNC_BRAR1L, LNC_BRAR1H }, 303 - { LNC_BRAR2L, LNC_BRAR2H } 65 + struct client { 66 + int (*client_callback)(void *client_data); 67 + void *customer_data; 68 + int client_called; 304 69 }; 305 70 306 - size_t const num_offsets = sizeof(offsets) / sizeof(offsets[0]); 307 - struct RAR_offsets const *end = offsets + num_offsets; 308 - struct RAR_offsets const *i; 309 - unsigned int n = 0; 310 - int result = 0; 71 + static DEFINE_MUTEX(rar_mutex); 72 + static DEFINE_MUTEX(lnc_reg_mutex); 311 73 312 - /* Retrieve RAR start and end physical addresses. */ 74 + struct RAR_device { 75 + struct RAR_offsets const rar_offsets[MRST_NUM_RAR]; 76 + struct RAR_address_range rar_addr[MRST_NUM_RAR]; 77 + struct pci_dev *rar_dev; 78 + bool registered; 79 + }; 313 80 81 + /* this platform has only one rar_device for 3 rar regions */ 82 + static struct RAR_device my_rar_device = { 83 + .rar_offsets = { 84 + [0].low = LNC_BRAR0L, 85 + [0].high = LNC_BRAR0H, 86 + [1].low = LNC_BRAR1L, 87 + [1].high = LNC_BRAR1H, 88 + [2].low = LNC_BRAR2L, 89 + [2].high = LNC_BRAR2H 90 + } 91 + }; 92 + 93 + /* this data is for handling requests from other drivers which arrive 94 + * prior to this driver initializing 95 + */ 96 + 97 + static struct client clients[MAX_RAR_CLIENTS]; 98 + static int num_clients; 99 + 100 + /* 101 + * This function is used to retrieved RAR info using the Lincroft 102 + * message bus interface. 103 + */ 104 + static int retrieve_rar_addr(struct pci_dev *pdev, 105 + int offset, 106 + dma_addr_t *addr) 107 + { 314 108 /* 315 - * Access the RAR registers through the Lincroft Message Bus 316 - * Interface on PCI device: 00:00.0 Host bridge. 317 - */ 109 + * ======== The Lincroft Message Bus Interface ======== 110 + * Lincroft registers may be obtained from the PCI 111 + * (the Host Bridge) using the Lincroft Message Bus 112 + * Interface. That message bus interface is generally 113 + * comprised of two registers: a control register (MCR, 0xDO) 114 + * and a data register (MDR, 0xD4). 115 + * 116 + * The MCR (message control register) format is the following: 117 + * 1. [31:24]: Opcode 118 + * 2. [23:16]: Port 119 + * 3. [15:8]: Register Offset 120 + * 4. [7:4]: Byte Enables (use 0xF to set all of these bits 121 + * to 1) 122 + * 5. [3:0]: reserved 123 + * 124 + * Read (0xD0) and write (0xE0) opcodes are written to the 125 + * control register when reading and writing to Lincroft 126 + * registers, respectively. 127 + * 128 + * We're interested in registers found in the Lincroft 129 + * B-unit. The B-unit port is 0x3. 130 + * 131 + * The six B-unit RAR register offsets we use are listed 132 + * earlier in this file. 133 + * 134 + * Lastly writing to the MCR register requires the "Byte 135 + * enables" bits to be set to 1. This may be achieved by 136 + * writing 0xF at bit 4. 137 + * 138 + * The MDR (message data register) format is the following: 139 + * 1. [31:0]: Read/Write Data 140 + * 141 + * Data being read from this register is only available after 142 + * writing the appropriate control message to the MCR 143 + * register. 144 + * 145 + * Data being written to this register must be written before 146 + * writing the appropriate control message to the MCR 147 + * register. 148 + */ 318 149 319 - /* struct pci_dev *pdev = pci_get_bus_and_slot(0, PCI_DEVFN(0,0)); */ 150 + int result; 320 151 321 - if (pdev == NULL) 322 - return -ENODEV; 152 + /* Construct control message */ 153 + u32 const message = 154 + (LNC_MESSAGE_READ_OPCODE << 24) 155 + | (LNC_BUNIT_PORT << 16) 156 + | (offset << 8) 157 + | (LNC_MESSAGE_BYTE_WRITE_ENABLES << 4); 323 158 324 - for (i = offsets; i != end; ++i, ++n) { 325 - if (memrar_get_rar_addr(pdev, 326 - (*i).low, 327 - &(rar_addr[n].low)) != 0 328 - || memrar_get_rar_addr(pdev, 329 - (*i).high, 330 - &(rar_addr[n].high)) != 0) { 331 - result = -1; 332 - break; 333 - } 159 + dev_dbg(&pdev->dev, "Offset for 'get' LNC MSG is %x\n", offset); 160 + 161 + if (addr == 0) { 162 + WARN_ON(1); 163 + return -EINVAL; 334 164 } 335 165 166 + /* 167 + * We synchronize access to the Lincroft MCR and MDR registers 168 + * until BOTH the command is issued through the MCR register 169 + * and the corresponding data is read from the MDR register. 170 + * Otherwise a race condition would exist between accesses to 171 + * both registers. 172 + */ 173 + 174 + mutex_lock(&lnc_reg_mutex); 175 + 176 + /* Send the control message */ 177 + result = pci_write_config_dword(pdev, LNC_MCR_OFFSET, message); 178 + 179 + dev_dbg(&pdev->dev, "Result from send ctl register is %x\n", result); 180 + 181 + if (!result) { 182 + result = pci_read_config_dword(pdev, LNC_MDR_OFFSET, 183 + (u32 *)addr); 184 + dev_dbg(&pdev->dev, 185 + "Result from read data register is %x\n", result); 186 + 187 + dev_dbg(&pdev->dev, 188 + "Value read from data register is %lx\n", 189 + (unsigned long)*addr); 190 + } 191 + 192 + mutex_unlock(&lnc_reg_mutex); 193 + 194 + return result; 195 + } 196 + 197 + static int set_rar_address(struct pci_dev *pdev, 198 + int offset, 199 + dma_addr_t addr) 200 + { 201 + /* 202 + * Data being written to this register must be written before 203 + * writing the appropriate control message to the MCR 204 + * register. 205 + * @note See rar_get_address() for a description of the 206 + * message bus interface being used here. 207 + */ 208 + 209 + int result = 0; 210 + 211 + /* Construct control message */ 212 + u32 const message = (LNC_MESSAGE_WRITE_OPCODE << 24) 213 + | (LNC_BUNIT_PORT << 16) 214 + | (offset << 8) 215 + | (LNC_MESSAGE_BYTE_WRITE_ENABLES << 4); 216 + 217 + if (addr == 0) { 218 + WARN_ON(1); 219 + return -EINVAL; 220 + } 221 + 222 + dev_dbg(&pdev->dev, "Offset for 'set' LNC MSG is %x\n", offset); 223 + 224 + /* 225 + * We synchronize access to the Lincroft MCR and MDR registers 226 + * until BOTH the command is issued through the MCR register 227 + * and the corresponding data is read from the MDR register. 228 + * Otherwise a race condition would exist between accesses to 229 + * both registers. 230 + */ 231 + 232 + mutex_lock(&lnc_reg_mutex); 233 + 234 + /* Send the control message */ 235 + result = pci_write_config_dword(pdev, LNC_MDR_OFFSET, addr); 236 + 237 + dev_dbg(&pdev->dev, "Result from write data register is %x\n", result); 238 + 239 + if (!result) { 240 + dev_dbg(&pdev->dev, 241 + "Value written to data register is %lx\n", 242 + (unsigned long)addr); 243 + 244 + result = pci_write_config_dword(pdev, LNC_MCR_OFFSET, message); 245 + 246 + dev_dbg(&pdev->dev, "Result from send ctl register is %x\n", 247 + result); 248 + } 249 + 250 + mutex_unlock(&lnc_reg_mutex); 251 + 252 + return result; 253 + } 254 + 255 + /* 256 + * Initialize RAR parameters, such as bus addresses, etc. 257 + */ 258 + static int init_rar_params(struct pci_dev *pdev) 259 + { 260 + unsigned int i; 261 + int result = 0; 262 + 263 + /* Retrieve RAR start and end bus addresses. 264 + * Access the RAR registers through the Lincroft Message Bus 265 + * Interface on PCI device: 00:00.0 Host bridge. 266 + */ 267 + 268 + for (i = 0; i < MRST_NUM_RAR; ++i) { 269 + struct RAR_offsets const *offset = 270 + &my_rar_device.rar_offsets[i]; 271 + struct RAR_address_range *addr = &my_rar_device.rar_addr[i]; 272 + 273 + if ((retrieve_rar_addr(pdev, offset->low, &addr->low) != 0) 274 + || (retrieve_rar_addr(pdev, offset->high, &addr->high) != 0)) { 275 + result = -1; 276 + break; 277 + } 278 + 279 + /* 280 + * Only the upper 22 bits of the RAR addresses are 281 + * stored in their corresponding RAR registers so we 282 + * must set the lower 10 bits accordingly. 283 + 284 + * The low address has its lower 10 bits cleared, and 285 + * the high address has all its lower 10 bits set, 286 + * e.g.: 287 + * low = 0x2ffffc00 288 + */ 289 + 290 + addr->low &= (dma_addr_t)0xfffffc00u; 291 + 292 + /* 293 + * Set bits 9:0 on uppser address if bits 31:10 are non 294 + * zero; otherwize clear all bits 295 + */ 296 + 297 + if ((addr->high & 0xfffffc00u) == 0) 298 + addr->high = 0; 299 + else 300 + addr->high |= 0x3ffu; 301 + } 336 302 /* Done accessing the device. */ 337 - /* pci_dev_put(pdev); */ 338 303 339 304 if (result == 0) { 340 - if (1) { 341 - size_t z; 342 - for (z = 0; z != MRST_NUM_RAR; ++z) { 343 - printk(KERN_WARNING 344 - "rar - BRAR[%Zd] physical address low\n" 345 - "\tlow: 0x%08x\n" 346 - "\thigh: 0x%08x\n", 347 - z, 348 - rar_addr[z].low, 349 - rar_addr[z].high); 350 - } 305 + int z; 306 + for (z = 0; z != MRST_NUM_RAR; ++z) { 307 + /* 308 + * "BRAR" refers to the RAR registers in the 309 + * Lincroft B-unit. 310 + */ 311 + dev_info(&pdev->dev, "BRAR[%u] bus address range = " 312 + "[%lx, %lx]\n", z, 313 + (unsigned long)my_rar_device.rar_addr[z].low, 314 + (unsigned long)my_rar_device.rar_addr[z].high); 351 315 } 352 316 } 353 317 ··· 353 321 } 354 322 355 323 /* 356 - function that is activated on the successfull probe of the RAR device 357 - */ 358 - static int __devinit rar_probe(struct pci_dev *pdev, 359 - const struct pci_device_id *ent) 324 + * The rar_get_address function is used by other device drivers 325 + * to obtain RAR address information on a RAR. It takes three 326 + * parameters: 327 + * 328 + * int rar_index 329 + * The rar_index is an index to the rar for which you wish to retrieve 330 + * the address information. 331 + * Values can be 0,1, or 2. 332 + * 333 + * The function returns a 0 upon success or a -1 if there is no RAR 334 + * facility on this system. 335 + */ 336 + int rar_get_address(int rar_index, 337 + dma_addr_t *start_address, 338 + dma_addr_t *end_address) 360 339 { 361 - /* error */ 340 + int result = -ENODEV; 341 + 342 + if (my_rar_device.registered) { 343 + if (start_address == 0 || end_address == 0 344 + || rar_index >= MRST_NUM_RAR || rar_index < 0) { 345 + result = -EINVAL; 346 + } else { 347 + *start_address = 348 + my_rar_device.rar_addr[rar_index].low; 349 + *end_address = 350 + my_rar_device.rar_addr[rar_index].high; 351 + 352 + result = 0; 353 + } 354 + } 355 + 356 + return result; 357 + } 358 + EXPORT_SYMBOL(rar_get_address); 359 + 360 + /* 361 + * The rar_lock function is ued by other device drivers to lock an RAR. 362 + * once an RAR is locked, it stays locked until the next system reboot. 363 + * The function takes one parameter: 364 + * 365 + * int rar_index 366 + * The rar_index is an index to the rar that you want to lock. 367 + * Values can be 0,1, or 2. 368 + * 369 + * The function returns a 0 upon success or a -1 if there is no RAR 370 + * facility on this system. 371 + */ 372 + int rar_lock(int rar_index) 373 + { 374 + int result = -ENODEV; 375 + 376 + if (rar_index >= MRST_NUM_RAR || rar_index < 0) { 377 + result = -EINVAL; 378 + return result; 379 + } 380 + 381 + dev_dbg(&my_rar_device.rar_dev->dev, "rar_lock mutex locking\n"); 382 + mutex_lock(&rar_mutex); 383 + 384 + if (my_rar_device.registered) { 385 + 386 + dma_addr_t low = my_rar_device.rar_addr[rar_index].low & 387 + 0xfffffc00u; 388 + 389 + dma_addr_t high = my_rar_device.rar_addr[rar_index].high & 390 + 0xfffffc00u; 391 + 392 + /* 393 + * Only allow I/O from the graphics and Langwell; 394 + * Not from the x96 processor 395 + */ 396 + if (rar_index == (int)RAR_TYPE_VIDEO) { 397 + low |= 0x00000009; 398 + high |= 0x00000015; 399 + } 400 + 401 + else if (rar_index == (int)RAR_TYPE_AUDIO) { 402 + /* Only allow I/O from Langwell; nothing from x86 */ 403 + low |= 0x00000008; 404 + high |= 0x00000018; 405 + } 406 + 407 + else 408 + /* Read-only from all agents */ 409 + high |= 0x00000018; 410 + 411 + /* 412 + * Now program the register using the Lincroft message 413 + * bus interface. 414 + */ 415 + result = set_rar_address(my_rar_device.rar_dev, 416 + my_rar_device.rar_offsets[rar_index].low, 417 + low); 418 + 419 + if (result == 0) 420 + result = set_rar_address( 421 + my_rar_device.rar_dev, 422 + my_rar_device.rar_offsets[rar_index].high, 423 + high); 424 + } 425 + 426 + dev_dbg(&my_rar_device.rar_dev->dev, "rar_lock mutex unlocking\n"); 427 + mutex_unlock(&rar_mutex); 428 + return result; 429 + } 430 + EXPORT_SYMBOL(rar_lock); 431 + 432 + /* The register_rar function is to used by other device drivers 433 + * to ensure that this driver is ready. As we cannot be sure of 434 + * the compile/execute order of dirvers in ther kernel, it is 435 + * best to give this driver a callback function to call when 436 + * it is ready to give out addresses. The callback function 437 + * would have those steps that continue the initialization of 438 + * a driver that do require a valid RAR address. One of those 439 + * steps would be to call rar_get_address() 440 + * This function return 0 on success an -1 on failure. 441 + */ 442 + int register_rar(int (*callback)(void *yourparameter), void *yourparameter) 443 + { 444 + 445 + int result = -ENODEV; 446 + 447 + if (callback == NULL) 448 + return -EINVAL; 449 + 450 + mutex_lock(&rar_mutex); 451 + 452 + if (my_rar_device.registered) { 453 + 454 + mutex_unlock(&rar_mutex); 455 + /* 456 + * if the driver already registered, then we can simply 457 + * call the callback right now 458 + */ 459 + 460 + return (*callback)(yourparameter); 461 + } 462 + 463 + if (num_clients < MRST_NUM_RAR) { 464 + 465 + clients[num_clients].client_callback = callback; 466 + clients[num_clients].customer_data = yourparameter; 467 + num_clients += 1; 468 + result = 0; 469 + } 470 + 471 + mutex_unlock(&rar_mutex); 472 + return result; 473 + 474 + } 475 + EXPORT_SYMBOL(register_rar); 476 + 477 + /* 478 + * This function registers the driver with the device subsystem ( 479 + * either PCI, USB, etc). 480 + * Function that is activaed on the succesful probe of the RAR device 481 + * (Moorestown host controller). 482 + */ 483 + static int rar_probe(struct pci_dev *dev, const struct pci_device_id *id) 484 + { 362 485 int error; 486 + int counter; 363 487 364 - /*------------------------ 365 - CODE 366 - ---------------------------*/ 367 - 368 - DEBUG_PRINT_0(RAR_DEBUG_LEVEL_EXTENDED, 369 - "Rar pci probe starting\n"); 370 - error = 0; 488 + dev_dbg(&dev->dev, "PCI probe starting\n"); 371 489 372 490 /* enable the device */ 373 - error = pci_enable_device(pdev); 491 + error = pci_enable_device(dev); 374 492 if (error) { 375 - DEBUG_PRINT_0(RAR_DEBUG_LEVEL_EXTENDED, 376 - "error enabling pci device\n"); 493 + dev_err(&dev->dev, 494 + "Error enabling RAR register PCI device\n"); 377 495 goto end_function; 378 496 } 379 497 380 - rar_dev = pdev; 381 - registered = 1; 498 + /* we have only one device; fill in the rar_device structure */ 499 + my_rar_device.rar_dev = dev; 382 500 383 - /* Initialize the RAR parameters, which have to be retrieved */ 384 - /* via the message bus service */ 385 - error = memrar_init_rar_params(rar_dev); 386 - 501 + /* 502 + * Initialize the RAR parameters, which have to be retrieved 503 + * via the message bus interface. 504 + */ 505 + error = init_rar_params(dev); 387 506 if (error) { 388 - DEBUG_PRINT_0(RAR_DEBUG_LEVEL_EXTENDED, 389 - "error getting RAR addresses device\n"); 390 - registered = 0; 507 + pci_disable_device(dev); 508 + 509 + dev_err(&dev->dev, 510 + "Error retrieving RAR addresses\n"); 511 + 391 512 goto end_function; 513 + } 514 + 515 + dev_dbg(&dev->dev, "PCI probe locking\n"); 516 + mutex_lock(&rar_mutex); 517 + my_rar_device.registered = 1; 518 + 519 + /* now call anyone who has registered (using callbacks) */ 520 + for (counter = 0; counter < num_clients; counter += 1) { 521 + if (clients[counter].client_callback) { 522 + error = (*clients[counter].client_callback)( 523 + clients[counter].customer_data); 524 + /* set callback to NULL to indicate it has been done */ 525 + clients[counter].client_callback = NULL; 526 + dev_dbg(&my_rar_device.rar_dev->dev, 527 + "Callback called for %d\n", 528 + counter); 392 529 } 530 + } 531 + 532 + dev_dbg(&dev->dev, "PCI probe unlocking\n"); 533 + mutex_unlock(&rar_mutex); 393 534 394 535 end_function: 395 536 396 537 return error; 397 538 } 398 539 399 - /* 400 - this function registers the driver to 401 - the device subsystem (either PCI, USB, etc) 402 - */ 540 + const struct pci_device_id rar_pci_id_tbl[] = { 541 + { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_RAR_DEVICE_ID) }, 542 + { 0 } 543 + }; 544 + 545 + MODULE_DEVICE_TABLE(pci, rar_pci_id_tbl); 546 + 547 + const struct pci_device_id *my_id_table = rar_pci_id_tbl; 548 + 549 + /* field for registering driver to PCI device */ 550 + static struct pci_driver rar_pci_driver = { 551 + .name = "rar_register_driver", 552 + .id_table = rar_pci_id_tbl, 553 + .probe = rar_probe 554 + }; 555 + 403 556 static int __init rar_init_handler(void) 404 557 { 405 558 return pci_register_driver(&rar_pci_driver); ··· 599 382 module_exit(rar_exit_handler); 600 383 601 384 MODULE_LICENSE("GPL"); 602 - 603 - 604 - /* The get_rar_address function is used by other device drivers 605 - * to obtain RAR address information on a RAR. It takes two 606 - * parameter: 607 - * 608 - * int rar_index 609 - * The rar_index is an index to the rar for which you wish to retrieve 610 - * the address information. 611 - * Values can be 0,1, or 2. 612 - * 613 - * struct RAR_address_struct is a pointer to a place to which the function 614 - * can return the address structure for the RAR. 615 - * 616 - * The function returns a 0 upon success or a -1 if there is no RAR 617 - * facility on this system. 618 - */ 619 - int get_rar_address(int rar_index, struct RAR_address_struct *addresses) 620 - { 621 - if (registered && (rar_index < 3) && (rar_index >= 0)) { 622 - *addresses = rar_addr[rar_index]; 623 - /* strip off lock bit information */ 624 - addresses->low = addresses->low & 0xfffffff0; 625 - addresses->high = addresses->high & 0xfffffff0; 626 - return 0; 627 - } else 628 - return -ENODEV; 629 - } 630 - EXPORT_SYMBOL(get_rar_address); 631 - 632 - /* The lock_rar function is used by other device drivers to lock an RAR. 633 - * once an RAR is locked, it stays locked until the next system reboot. 634 - * The function takes one parameter: 635 - * 636 - * int rar_index 637 - * The rar_index is an index to the rar that you want to lock. 638 - * Values can be 0,1, or 2. 639 - * 640 - * The function returns a 0 upon success or a -1 if there is no RAR 641 - * facility on this system. 642 - */ 643 - int lock_rar(int rar_index) 644 - { 645 - u32 working_addr; 646 - int result; 647 - 648 - if (registered && (rar_index < 3) && (rar_index >= 0)) { 649 - /* first make sure that lock bits are clear (this does lock) */ 650 - working_addr = rar_addr[rar_index].low & 0xfffffff0; 651 - 652 - /* now send that value to the register using the IPC */ 653 - result = memrar_set_rar_addr(rar_dev, rar_index, working_addr); 654 - return result; 655 - } else 656 - return -ENODEV; 657 - } 385 + MODULE_DESCRIPTION("Intel Restricted Access Region Register Driver");