You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pci.c 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * Based in part on pci.c from Etherboot 5.4, by Ken Yap and David
  5. * Munro, in turn based on the Linux kernel's PCI implementation.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of the
  10. * License, or any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <stdint.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <malloc.h>
  25. #include <gpxe/tables.h>
  26. #include <gpxe/device.h>
  27. #include <gpxe/pci.h>
  28. /** @file
  29. *
  30. * PCI bus
  31. *
  32. */
  33. static struct pci_driver pci_drivers[0] __table_start ( pci_drivers );
  34. static struct pci_driver pci_drivers_end[0] __table_end ( pci_drivers );
  35. static void pcibus_remove ( struct root_device *rootdev );
  36. /**
  37. * Read PCI BAR
  38. *
  39. * @v pci PCI device
  40. * @v reg PCI register number
  41. * @ret bar Base address register
  42. *
  43. * Reads the specified PCI base address register, including the flags
  44. * portion. 64-bit BARs will be handled automatically. If the value
  45. * of the 64-bit BAR exceeds the size of an unsigned long (i.e. if the
  46. * high dword is non-zero on a 32-bit platform), then the value
  47. * returned will be zero plus the flags for a 64-bit BAR. Unreachable
  48. * 64-bit BARs are therefore returned as uninitialised 64-bit BARs.
  49. */
  50. static unsigned long pci_bar ( struct pci_device *pci, unsigned int reg ) {
  51. uint32_t low;
  52. uint32_t high;
  53. pci_read_config_dword ( pci, reg, &low );
  54. if ( ( low & (PCI_BASE_ADDRESS_SPACE|PCI_BASE_ADDRESS_MEM_TYPE_MASK) )
  55. == (PCI_BASE_ADDRESS_SPACE_MEMORY|PCI_BASE_ADDRESS_MEM_TYPE_64) ){
  56. pci_read_config_dword ( pci, reg + 4, &high );
  57. if ( high ) {
  58. if ( sizeof ( unsigned long ) > sizeof ( uint32_t ) ) {
  59. return ( ( ( uint64_t ) high << 32 ) | low );
  60. } else {
  61. DBG ( "Unhandled 64-bit BAR %08x%08x\n",
  62. high, low );
  63. return PCI_BASE_ADDRESS_MEM_TYPE_64;
  64. }
  65. }
  66. }
  67. return low;
  68. }
  69. /**
  70. * Find the start of a PCI BAR
  71. *
  72. * @v pci PCI device
  73. * @v reg PCI register number
  74. * @ret start BAR start address
  75. *
  76. * Reads the specified PCI base address register, and returns the
  77. * address portion of the BAR (i.e. without the flags).
  78. *
  79. * If the address exceeds the size of an unsigned long (i.e. if a
  80. * 64-bit BAR has a non-zero high dword on a 32-bit machine), the
  81. * return value will be zero.
  82. */
  83. unsigned long pci_bar_start ( struct pci_device *pci, unsigned int reg ) {
  84. unsigned long bar;
  85. bar = pci_bar ( pci, reg );
  86. if ( (bar & PCI_BASE_ADDRESS_SPACE) == PCI_BASE_ADDRESS_SPACE_MEMORY ){
  87. return ( bar & PCI_BASE_ADDRESS_MEM_MASK );
  88. } else {
  89. return ( bar & PCI_BASE_ADDRESS_IO_MASK );
  90. }
  91. }
  92. /**
  93. * Read membase and ioaddr for a PCI device
  94. *
  95. * @v pci PCI device
  96. *
  97. * This scans through all PCI BARs on the specified device. The first
  98. * valid memory BAR is recorded as pci_device::membase, and the first
  99. * valid IO BAR is recorded as pci_device::ioaddr.
  100. *
  101. * 64-bit BARs are handled automatically. On a 32-bit platform, if a
  102. * 64-bit BAR has a non-zero high dword, it will be regarded as
  103. * invalid.
  104. */
  105. static void pci_read_bases ( struct pci_device *pci ) {
  106. unsigned long bar;
  107. int reg;
  108. for ( reg = PCI_BASE_ADDRESS_0; reg <= PCI_BASE_ADDRESS_5; reg += 4 ) {
  109. bar = pci_bar ( pci, reg );
  110. if ( bar & PCI_BASE_ADDRESS_SPACE_IO ) {
  111. if ( ! pci->ioaddr )
  112. pci->ioaddr =
  113. ( bar & PCI_BASE_ADDRESS_IO_MASK );
  114. } else {
  115. if ( ! pci->membase )
  116. pci->membase =
  117. ( bar & PCI_BASE_ADDRESS_MEM_MASK );
  118. /* Skip next BAR if 64-bit */
  119. if ( bar & PCI_BASE_ADDRESS_MEM_TYPE_64 )
  120. reg += 4;
  121. }
  122. }
  123. }
  124. /**
  125. * Enable PCI device
  126. *
  127. * @v pci PCI device
  128. *
  129. * Set device to be a busmaster in case BIOS neglected to do so. Also
  130. * adjust PCI latency timer to a reasonable value, 32.
  131. */
  132. void adjust_pci_device ( struct pci_device *pci ) {
  133. unsigned short new_command, pci_command;
  134. unsigned char pci_latency;
  135. pci_read_config_word ( pci, PCI_COMMAND, &pci_command );
  136. new_command = pci_command | PCI_COMMAND_MASTER | PCI_COMMAND_IO;
  137. if ( pci_command != new_command ) {
  138. DBG ( "PCI BIOS has not enabled device %02x:%02x.%x! "
  139. "Updating PCI command %04x->%04x\n", pci->bus,
  140. PCI_SLOT ( pci->devfn ), PCI_FUNC ( pci->devfn ),
  141. pci_command, new_command );
  142. pci_write_config_word ( pci, PCI_COMMAND, new_command );
  143. }
  144. pci_read_config_byte ( pci, PCI_LATENCY_TIMER, &pci_latency);
  145. if ( pci_latency < 32 ) {
  146. DBG ( "PCI device %02x:%02x.%x latency timer is unreasonably "
  147. "low at %d. Setting to 32.\n", pci->bus,
  148. PCI_SLOT ( pci->devfn ), PCI_FUNC ( pci->devfn ),
  149. pci_latency );
  150. pci_write_config_byte ( pci, PCI_LATENCY_TIMER, 32);
  151. }
  152. }
  153. /**
  154. * Register PCI device
  155. *
  156. * @v pci PCI device
  157. * @ret rc Return status code
  158. *
  159. * Searches for a driver for the PCI device. If a driver is found,
  160. * its probe() routine is called, and the device is added to the
  161. * device hierarchy.
  162. */
  163. static int register_pcidev ( struct pci_device *pci ) {
  164. struct pci_driver *driver;
  165. struct pci_device_id *id;
  166. unsigned int i;
  167. int rc;
  168. DBG ( "Registering PCI device %02x:%02x.%x (%04x:%04x mem %lx "
  169. "io %lx irq %d)\n", pci->bus, PCI_SLOT ( pci->devfn ),
  170. PCI_FUNC ( pci->devfn ), pci->vendor, pci->device,
  171. pci->membase, pci->ioaddr, pci->irq );
  172. for ( driver = pci_drivers ; driver < pci_drivers_end ; driver++ ) {
  173. for ( i = 0 ; i < driver->id_count ; i++ ) {
  174. id = &driver->ids[i];
  175. if ( ( id->vendor != pci->vendor ) ||
  176. ( id->device != pci->device ) )
  177. continue;
  178. pci->driver = driver;
  179. pci->name = id->name;
  180. DBG ( "...using driver %s\n", pci->name );
  181. if ( ( rc = driver->probe ( pci, id ) ) != 0 ) {
  182. DBG ( "......probe failed\n" );
  183. continue;
  184. }
  185. list_add ( &pci->dev.siblings,
  186. &pci->dev.parent->children );
  187. return 0;
  188. }
  189. }
  190. DBG ( "...no driver found\n" );
  191. return -ENOTTY;
  192. }
  193. /**
  194. * Unregister a PCI device
  195. *
  196. * @v pci PCI device
  197. *
  198. * Calls the device's driver's remove() routine, and removes the
  199. * device from the device hierarchy.
  200. */
  201. static void unregister_pcidev ( struct pci_device *pci ) {
  202. pci->driver->remove ( pci );
  203. list_del ( &pci->dev.siblings );
  204. DBG ( "Unregistered PCI device %02x:%02x.%x\n", pci->bus,
  205. PCI_SLOT ( pci->devfn ), PCI_FUNC ( pci->devfn ) );
  206. }
  207. /**
  208. * Probe PCI root bus
  209. *
  210. * @v rootdev PCI bus root device
  211. *
  212. * Scans the PCI bus for devices and registers all devices it can
  213. * find.
  214. */
  215. static int pcibus_probe ( struct root_device *rootdev ) {
  216. struct pci_device *pci = NULL;
  217. unsigned int max_bus;
  218. unsigned int bus;
  219. unsigned int devfn;
  220. uint8_t hdrtype = 0;
  221. uint32_t tmp;
  222. int rc;
  223. max_bus = pci_max_bus();
  224. for ( bus = 0 ; bus <= max_bus ; bus++ ) {
  225. for ( devfn = 0 ; devfn <= 0xff ; devfn++ ) {
  226. /* Allocate struct pci_device */
  227. if ( ! pci )
  228. pci = malloc ( sizeof ( *pci ) );
  229. if ( ! pci ) {
  230. rc = -ENOMEM;
  231. goto err;
  232. }
  233. memset ( pci, 0, sizeof ( *pci ) );
  234. pci->bus = bus;
  235. pci->devfn = devfn;
  236. /* Skip all but the first function on
  237. * non-multifunction cards
  238. */
  239. if ( PCI_FUNC ( devfn ) == 0 ) {
  240. pci_read_config_byte ( pci, PCI_HEADER_TYPE,
  241. &hdrtype );
  242. } else if ( ! ( hdrtype & 0x80 ) ) {
  243. continue;
  244. }
  245. /* Check for physical device presence */
  246. pci_read_config_dword ( pci, PCI_VENDOR_ID, &tmp );
  247. if ( ( tmp == 0xffffffff ) || ( tmp == 0 ) )
  248. continue;
  249. /* Populate struct pci_device */
  250. pci->vendor = ( tmp & 0xffff );
  251. pci->device = ( tmp >> 16 );
  252. pci_read_config_dword ( pci, PCI_REVISION, &tmp );
  253. pci->class = ( tmp >> 8 );
  254. pci_read_config_byte ( pci, PCI_INTERRUPT_LINE,
  255. &pci->irq );
  256. pci_read_bases ( pci );
  257. INIT_LIST_HEAD ( &pci->dev.children );
  258. pci->dev.parent = &rootdev->dev;
  259. /* Look for a driver */
  260. if ( register_pcidev ( pci ) == 0 ) {
  261. /* pcidev registered, we can drop our ref */
  262. pci = NULL;
  263. } else {
  264. /* Not registered; re-use struct pci_device */
  265. }
  266. }
  267. }
  268. free ( pci );
  269. return 0;
  270. err:
  271. free ( pci );
  272. pcibus_remove ( rootdev );
  273. return rc;
  274. }
  275. /**
  276. * Remove PCI root bus
  277. *
  278. * @v rootdev PCI bus root device
  279. */
  280. static void pcibus_remove ( struct root_device *rootdev ) {
  281. struct pci_device *pci;
  282. struct pci_device *tmp;
  283. list_for_each_entry_safe ( pci, tmp, &rootdev->dev.children,
  284. dev.siblings ) {
  285. unregister_pcidev ( pci );
  286. free ( pci );
  287. }
  288. }
  289. /** PCI bus root device driver */
  290. static struct root_driver pci_root_driver = {
  291. .probe = pcibus_probe,
  292. .remove = pcibus_remove,
  293. };
  294. /** PCI bus root device */
  295. struct root_device pci_root_device __root_device = {
  296. .name = "PCI",
  297. .driver = &pci_root_driver,
  298. .dev = {
  299. .children = LIST_HEAD_INIT ( pci_root_device.dev.children ),
  300. },
  301. };