PageRenderTime 1297ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/platform/x86/intel_rar_register.c

https://github.com/Mengqi/linux-2.6
C | 669 lines | 287 code | 82 blank | 300 comment | 46 complexity | fde19c2b0150041b010af5d5930d6ce1 MD5 | raw file
  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. #include <linux/module.h>
  43. #include <linux/pci.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/device.h>
  46. #include <linux/kernel.h>
  47. #include <linux/rar_register.h>
  48. /* === Lincroft Message Bus Interface === */
  49. #define LNC_MCR_OFFSET 0xD0 /* Message Control Register */
  50. #define LNC_MDR_OFFSET 0xD4 /* Message Data Register */
  51. /* Message Opcodes */
  52. #define LNC_MESSAGE_READ_OPCODE 0xD0
  53. #define LNC_MESSAGE_WRITE_OPCODE 0xE0
  54. /* Message Write Byte Enables */
  55. #define LNC_MESSAGE_BYTE_WRITE_ENABLES 0xF
  56. /* B-unit Port */
  57. #define LNC_BUNIT_PORT 0x3
  58. /* === Lincroft B-Unit Registers - Programmed by IA32 firmware === */
  59. #define LNC_BRAR0L 0x10
  60. #define LNC_BRAR0H 0x11
  61. #define LNC_BRAR1L 0x12
  62. #define LNC_BRAR1H 0x13
  63. /* Reserved for SeP */
  64. #define LNC_BRAR2L 0x14
  65. #define LNC_BRAR2H 0x15
  66. /* Moorestown supports three restricted access regions. */
  67. #define MRST_NUM_RAR 3
  68. /* RAR Bus Address Range */
  69. struct rar_addr {
  70. dma_addr_t low;
  71. dma_addr_t high;
  72. };
  73. /*
  74. * We create one of these for each RAR
  75. */
  76. struct client {
  77. int (*callback)(unsigned long data);
  78. unsigned long driver_priv;
  79. bool busy;
  80. };
  81. static DEFINE_MUTEX(rar_mutex);
  82. static DEFINE_MUTEX(lnc_reg_mutex);
  83. /*
  84. * One per RAR device (currently only one device)
  85. */
  86. struct rar_device {
  87. struct rar_addr rar_addr[MRST_NUM_RAR];
  88. struct pci_dev *rar_dev;
  89. bool registered;
  90. bool allocated;
  91. struct client client[MRST_NUM_RAR];
  92. };
  93. /* Current platforms have only one rar_device for 3 rar regions */
  94. static struct rar_device my_rar_device;
  95. /*
  96. * Abstract out multiple device support. Current platforms only
  97. * have a single RAR device.
  98. */
  99. /**
  100. * alloc_rar_device - return a new RAR structure
  101. *
  102. * Return a new (but not yet ready) RAR device object
  103. */
  104. static struct rar_device *alloc_rar_device(void)
  105. {
  106. if (my_rar_device.allocated)
  107. return NULL;
  108. my_rar_device.allocated = 1;
  109. return &my_rar_device;
  110. }
  111. /**
  112. * free_rar_device - free a RAR object
  113. * @rar: the RAR device being freed
  114. *
  115. * Release a RAR object and any attached resources
  116. */
  117. static void free_rar_device(struct rar_device *rar)
  118. {
  119. pci_dev_put(rar->rar_dev);
  120. rar->allocated = 0;
  121. }
  122. /**
  123. * _rar_to_device - return the device handling this RAR
  124. * @rar: RAR number
  125. * @off: returned offset
  126. *
  127. * Internal helper for looking up RAR devices. This and alloc are the
  128. * two functions that need touching to go to multiple RAR devices.
  129. */
  130. static struct rar_device *_rar_to_device(int rar, int *off)
  131. {
  132. if (rar >= 0 && rar < MRST_NUM_RAR) {
  133. *off = rar;
  134. return &my_rar_device;
  135. }
  136. return NULL;
  137. }
  138. /**
  139. * rar_to_device - return the device handling this RAR
  140. * @rar: RAR number
  141. * @off: returned offset
  142. *
  143. * Return the device this RAR maps to if one is present, otherwise
  144. * returns NULL. Reports the offset relative to the base of this
  145. * RAR device in off.
  146. */
  147. static struct rar_device *rar_to_device(int rar, int *off)
  148. {
  149. struct rar_device *rar_dev = _rar_to_device(rar, off);
  150. if (rar_dev == NULL || !rar_dev->registered)
  151. return NULL;
  152. return rar_dev;
  153. }
  154. /**
  155. * rar_to_client - return the client handling this RAR
  156. * @rar: RAR number
  157. *
  158. * Return the client this RAR maps to if a mapping is known, otherwise
  159. * returns NULL.
  160. */
  161. static struct client *rar_to_client(int rar)
  162. {
  163. int idx;
  164. struct rar_device *r = _rar_to_device(rar, &idx);
  165. if (r != NULL)
  166. return &r->client[idx];
  167. return NULL;
  168. }
  169. /**
  170. * rar_read_addr - retrieve a RAR mapping
  171. * @pdev: PCI device for the RAR
  172. * @offset: offset for message
  173. * @addr: returned address
  174. *
  175. * Reads the address of a given RAR register. Returns 0 on success
  176. * or an error code on failure.
  177. */
  178. static int rar_read_addr(struct pci_dev *pdev, int offset, dma_addr_t *addr)
  179. {
  180. /*
  181. * ======== The Lincroft Message Bus Interface ========
  182. * Lincroft registers may be obtained via PCI from
  183. * the host bridge using the Lincroft Message Bus
  184. * Interface. That message bus interface is generally
  185. * comprised of two registers: a control register (MCR, 0xDO)
  186. * and a data register (MDR, 0xD4).
  187. *
  188. * The MCR (message control register) format is the following:
  189. * 1. [31:24]: Opcode
  190. * 2. [23:16]: Port
  191. * 3. [15:8]: Register Offset
  192. * 4. [7:4]: Byte Enables (use 0xF to set all of these bits
  193. * to 1)
  194. * 5. [3:0]: reserved
  195. *
  196. * Read (0xD0) and write (0xE0) opcodes are written to the
  197. * control register when reading and writing to Lincroft
  198. * registers, respectively.
  199. *
  200. * We're interested in registers found in the Lincroft
  201. * B-unit. The B-unit port is 0x3.
  202. *
  203. * The six B-unit RAR register offsets we use are listed
  204. * earlier in this file.
  205. *
  206. * Lastly writing to the MCR register requires the "Byte
  207. * enables" bits to be set to 1. This may be achieved by
  208. * writing 0xF at bit 4.
  209. *
  210. * The MDR (message data register) format is the following:
  211. * 1. [31:0]: Read/Write Data
  212. *
  213. * Data being read from this register is only available after
  214. * writing the appropriate control message to the MCR
  215. * register.
  216. *
  217. * Data being written to this register must be written before
  218. * writing the appropriate control message to the MCR
  219. * register.
  220. */
  221. int result;
  222. u32 addr32;
  223. /* Construct control message */
  224. u32 const message =
  225. (LNC_MESSAGE_READ_OPCODE << 24)
  226. | (LNC_BUNIT_PORT << 16)
  227. | (offset << 8)
  228. | (LNC_MESSAGE_BYTE_WRITE_ENABLES << 4);
  229. dev_dbg(&pdev->dev, "Offset for 'get' LNC MSG is %x\n", offset);
  230. /*
  231. * We synchronize access to the Lincroft MCR and MDR registers
  232. * until BOTH the command is issued through the MCR register
  233. * and the corresponding data is read from the MDR register.
  234. * Otherwise a race condition would exist between accesses to
  235. * both registers.
  236. */
  237. mutex_lock(&lnc_reg_mutex);
  238. /* Send the control message */
  239. result = pci_write_config_dword(pdev, LNC_MCR_OFFSET, message);
  240. if (!result) {
  241. /* Read back the address as a 32bit value */
  242. result = pci_read_config_dword(pdev, LNC_MDR_OFFSET, &addr32);
  243. *addr = (dma_addr_t)addr32;
  244. }
  245. mutex_unlock(&lnc_reg_mutex);
  246. return result;
  247. }
  248. /**
  249. * rar_set_addr - Set a RAR mapping
  250. * @pdev: PCI device for the RAR
  251. * @offset: offset for message
  252. * @addr: address to set
  253. *
  254. * Sets the address of a given RAR register. Returns 0 on success
  255. * or an error code on failure.
  256. */
  257. static int rar_set_addr(struct pci_dev *pdev,
  258. int offset,
  259. dma_addr_t addr)
  260. {
  261. /*
  262. * Data being written to this register must be written before
  263. * writing the appropriate control message to the MCR
  264. * register.
  265. * See rar_get_addrs() for a description of the
  266. * message bus interface being used here.
  267. */
  268. int result;
  269. /* Construct control message */
  270. u32 const message = (LNC_MESSAGE_WRITE_OPCODE << 24)
  271. | (LNC_BUNIT_PORT << 16)
  272. | (offset << 8)
  273. | (LNC_MESSAGE_BYTE_WRITE_ENABLES << 4);
  274. /*
  275. * We synchronize access to the Lincroft MCR and MDR registers
  276. * until BOTH the command is issued through the MCR register
  277. * and the corresponding data is read from the MDR register.
  278. * Otherwise a race condition would exist between accesses to
  279. * both registers.
  280. */
  281. mutex_lock(&lnc_reg_mutex);
  282. /* Send the control message */
  283. result = pci_write_config_dword(pdev, LNC_MDR_OFFSET, addr);
  284. if (!result)
  285. /* And address */
  286. result = pci_write_config_dword(pdev, LNC_MCR_OFFSET, message);
  287. mutex_unlock(&lnc_reg_mutex);
  288. return result;
  289. }
  290. /*
  291. * rar_init_params - Initialize RAR parameters
  292. * @rar: RAR device to initialise
  293. *
  294. * Initialize RAR parameters, such as bus addresses, etc. Returns 0
  295. * on success, or an error code on failure.
  296. */
  297. static int init_rar_params(struct rar_device *rar)
  298. {
  299. struct pci_dev *pdev = rar->rar_dev;
  300. unsigned int i;
  301. int result = 0;
  302. int offset = 0x10; /* RAR 0 to 2 in order low/high/low/high/... */
  303. /* Retrieve RAR start and end bus addresses.
  304. * Access the RAR registers through the Lincroft Message Bus
  305. * Interface on PCI device: 00:00.0 Host bridge.
  306. */
  307. for (i = 0; i < MRST_NUM_RAR; ++i) {
  308. struct rar_addr *addr = &rar->rar_addr[i];
  309. result = rar_read_addr(pdev, offset++, &addr->low);
  310. if (result != 0)
  311. return result;
  312. result = rar_read_addr(pdev, offset++, &addr->high);
  313. if (result != 0)
  314. return result;
  315. /*
  316. * Only the upper 22 bits of the RAR addresses are
  317. * stored in their corresponding RAR registers so we
  318. * must set the lower 10 bits accordingly.
  319. * The low address has its lower 10 bits cleared, and
  320. * the high address has all its lower 10 bits set,
  321. * e.g.:
  322. * low = 0x2ffffc00
  323. */
  324. addr->low &= (dma_addr_t)0xfffffc00u;
  325. /*
  326. * Set bits 9:0 on uppser address if bits 31:10 are non
  327. * zero; otherwize clear all bits
  328. */
  329. if ((addr->high & 0xfffffc00u) == 0)
  330. addr->high = 0;
  331. else
  332. addr->high |= 0x3ffu;
  333. }
  334. /* Done accessing the device. */
  335. if (result == 0) {
  336. for (i = 0; i != MRST_NUM_RAR; ++i) {
  337. /*
  338. * "BRAR" refers to the RAR registers in the
  339. * Lincroft B-unit.
  340. */
  341. dev_info(&pdev->dev, "BRAR[%u] bus address range = "
  342. "[%lx, %lx]\n", i,
  343. (unsigned long)rar->rar_addr[i].low,
  344. (unsigned long)rar->rar_addr[i].high);
  345. }
  346. }
  347. return result;
  348. }
  349. /**
  350. * rar_get_address - get the bus address in a RAR
  351. * @start: return value of start address of block
  352. * @end: return value of end address of block
  353. *
  354. * The rar_get_address function is used by other device drivers
  355. * to obtain RAR address information on a RAR. It takes three
  356. * parameters:
  357. *
  358. * The function returns a 0 upon success or an error if there is no RAR
  359. * facility on this system.
  360. */
  361. int rar_get_address(int rar_index, dma_addr_t *start, dma_addr_t *end)
  362. {
  363. int idx;
  364. struct rar_device *rar = rar_to_device(rar_index, &idx);
  365. if (rar == NULL) {
  366. WARN_ON(1);
  367. return -ENODEV;
  368. }
  369. *start = rar->rar_addr[idx].low;
  370. *end = rar->rar_addr[idx].high;
  371. return 0;
  372. }
  373. EXPORT_SYMBOL(rar_get_address);
  374. /**
  375. * rar_lock - lock a RAR register
  376. * @rar_index: RAR to lock (0-2)
  377. *
  378. * The rar_lock function is ued by other device drivers to lock an RAR.
  379. * once a RAR is locked, it stays locked until the next system reboot.
  380. *
  381. * The function returns a 0 upon success or an error if there is no RAR
  382. * facility on this system, or the locking fails
  383. */
  384. int rar_lock(int rar_index)
  385. {
  386. struct rar_device *rar;
  387. int result;
  388. int idx;
  389. dma_addr_t low, high;
  390. rar = rar_to_device(rar_index, &idx);
  391. if (rar == NULL) {
  392. WARN_ON(1);
  393. return -EINVAL;
  394. }
  395. low = rar->rar_addr[idx].low & 0xfffffc00u;
  396. high = rar->rar_addr[idx].high & 0xfffffc00u;
  397. /*
  398. * Only allow I/O from the graphics and Langwell;
  399. * not from the x86 processor
  400. */
  401. if (rar_index == RAR_TYPE_VIDEO) {
  402. low |= 0x00000009;
  403. high |= 0x00000015;
  404. } else if (rar_index == RAR_TYPE_AUDIO) {
  405. /* Only allow I/O from Langwell; nothing from x86 */
  406. low |= 0x00000008;
  407. high |= 0x00000018;
  408. } else
  409. /* Read-only from all agents */
  410. high |= 0x00000018;
  411. /*
  412. * Now program the register using the Lincroft message
  413. * bus interface.
  414. */
  415. result = rar_set_addr(rar->rar_dev,
  416. 2 * idx, low);
  417. if (result == 0)
  418. result = rar_set_addr(rar->rar_dev,
  419. 2 * idx + 1, high);
  420. return result;
  421. }
  422. EXPORT_SYMBOL(rar_lock);
  423. /**
  424. * register_rar - register a RAR handler
  425. * @num: RAR we wish to register for
  426. * @callback: function to call when RAR support is available
  427. * @data: data to pass to this function
  428. *
  429. * The register_rar function is to used by other device drivers
  430. * to ensure that this driver is ready. As we cannot be sure of
  431. * the compile/execute order of drivers in the kernel, it is
  432. * best to give this driver a callback function to call when
  433. * it is ready to give out addresses. The callback function
  434. * would have those steps that continue the initialization of
  435. * a driver that do require a valid RAR address. One of those
  436. * steps would be to call rar_get_address()
  437. *
  438. * This function return 0 on success or an error code on failure.
  439. */
  440. int register_rar(int num, int (*callback)(unsigned long data),
  441. unsigned long data)
  442. {
  443. /* For now we hardcode a single RAR device */
  444. struct rar_device *rar;
  445. struct client *c;
  446. int idx;
  447. int retval = 0;
  448. mutex_lock(&rar_mutex);
  449. /* Do we have a client mapping for this RAR number ? */
  450. c = rar_to_client(num);
  451. if (c == NULL) {
  452. retval = -ERANGE;
  453. goto done;
  454. }
  455. /* Is it claimed ? */
  456. if (c->busy) {
  457. retval = -EBUSY;
  458. goto done;
  459. }
  460. c->busy = 1;
  461. /* See if we have a handler for this RAR yet, if we do then fire it */
  462. rar = rar_to_device(num, &idx);
  463. if (rar) {
  464. /*
  465. * if the driver already registered, then we can simply
  466. * call the callback right now
  467. */
  468. (*callback)(data);
  469. goto done;
  470. }
  471. /* Arrange to be called back when the hardware is found */
  472. c->callback = callback;
  473. c->driver_priv = data;
  474. done:
  475. mutex_unlock(&rar_mutex);
  476. return retval;
  477. }
  478. EXPORT_SYMBOL(register_rar);
  479. /**
  480. * unregister_rar - release a RAR allocation
  481. * @num: RAR number
  482. *
  483. * Releases a RAR allocation, or pending allocation. If a callback is
  484. * pending then this function will either complete before the unregister
  485. * returns or not at all.
  486. */
  487. void unregister_rar(int num)
  488. {
  489. struct client *c;
  490. mutex_lock(&rar_mutex);
  491. c = rar_to_client(num);
  492. if (c == NULL || !c->busy)
  493. WARN_ON(1);
  494. else
  495. c->busy = 0;
  496. mutex_unlock(&rar_mutex);
  497. }
  498. EXPORT_SYMBOL(unregister_rar);
  499. /**
  500. * rar_callback - Process callbacks
  501. * @rar: new RAR device
  502. *
  503. * Process the callbacks for a newly found RAR device.
  504. */
  505. static void rar_callback(struct rar_device *rar)
  506. {
  507. struct client *c = &rar->client[0];
  508. int i;
  509. mutex_lock(&rar_mutex);
  510. rar->registered = 1; /* Ensure no more callbacks queue */
  511. for (i = 0; i < MRST_NUM_RAR; i++) {
  512. if (c->callback && c->busy) {
  513. c->callback(c->driver_priv);
  514. c->callback = NULL;
  515. }
  516. c++;
  517. }
  518. mutex_unlock(&rar_mutex);
  519. }
  520. /**
  521. * rar_probe - PCI probe callback
  522. * @dev: PCI device
  523. * @id: matching entry in the match table
  524. *
  525. * A RAR device has been discovered. Initialise it and if successful
  526. * process any pending callbacks that can now be completed.
  527. */
  528. static int rar_probe(struct pci_dev *dev, const struct pci_device_id *id)
  529. {
  530. int error;
  531. struct rar_device *rar;
  532. dev_dbg(&dev->dev, "PCI probe starting\n");
  533. rar = alloc_rar_device();
  534. if (rar == NULL)
  535. return -EBUSY;
  536. /* Enable the device */
  537. error = pci_enable_device(dev);
  538. if (error) {
  539. dev_err(&dev->dev,
  540. "Error enabling RAR register PCI device\n");
  541. goto end_function;
  542. }
  543. /* Fill in the rar_device structure */
  544. rar->rar_dev = pci_dev_get(dev);
  545. pci_set_drvdata(dev, rar);
  546. /*
  547. * Initialize the RAR parameters, which have to be retrieved
  548. * via the message bus interface.
  549. */
  550. error = init_rar_params(rar);
  551. if (error) {
  552. pci_disable_device(dev);
  553. dev_err(&dev->dev, "Error retrieving RAR addresses\n");
  554. goto end_function;
  555. }
  556. /* now call anyone who has registered (using callbacks) */
  557. rar_callback(rar);
  558. return 0;
  559. end_function:
  560. free_rar_device(rar);
  561. return error;
  562. }
  563. static DEFINE_PCI_DEVICE_TABLE(rar_pci_id_tbl) = {
  564. { PCI_VDEVICE(INTEL, 0x4110) },
  565. { 0 }
  566. };
  567. MODULE_DEVICE_TABLE(pci, rar_pci_id_tbl);
  568. /* field for registering driver to PCI device */
  569. static struct pci_driver rar_pci_driver = {
  570. .name = "rar_register_driver",
  571. .id_table = rar_pci_id_tbl,
  572. .probe = rar_probe,
  573. /* Cannot be unplugged - no remove */
  574. };
  575. static int __init rar_init_handler(void)
  576. {
  577. return pci_register_driver(&rar_pci_driver);
  578. }
  579. static void __exit rar_exit_handler(void)
  580. {
  581. pci_unregister_driver(&rar_pci_driver);
  582. }
  583. module_init(rar_init_handler);
  584. module_exit(rar_exit_handler);
  585. MODULE_LICENSE("GPL");
  586. MODULE_DESCRIPTION("Intel Restricted Access Region Register Driver");