common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. * Low-Level PCI Support for PC
  3. *
  4. * (c) 1999--2000 Martin Mares <mj@ucw.cz>
  5. */
  6. #include <linux/sched.h>
  7. #include <linux/pci.h>
  8. #include <linux/ioport.h>
  9. #include <linux/init.h>
  10. #include <linux/dmi.h>
  11. #include <asm/acpi.h>
  12. #include <asm/segment.h>
  13. #include <asm/io.h>
  14. #include <asm/smp.h>
  15. #include "pci.h"
  16. unsigned int pci_probe = PCI_PROBE_BIOS | PCI_PROBE_CONF1 | PCI_PROBE_CONF2 |
  17. PCI_PROBE_MMCONF;
  18. static int pci_bf_sort;
  19. int pci_routeirq;
  20. int pcibios_last_bus = -1;
  21. unsigned long pirq_table_addr;
  22. struct pci_bus *pci_root_bus;
  23. struct pci_raw_ops *raw_pci_ops;
  24. static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
  25. {
  26. return raw_pci_ops->read(pci_domain_nr(bus), bus->number,
  27. devfn, where, size, value);
  28. }
  29. static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
  30. {
  31. return raw_pci_ops->write(pci_domain_nr(bus), bus->number,
  32. devfn, where, size, value);
  33. }
  34. struct pci_ops pci_root_ops = {
  35. .read = pci_read,
  36. .write = pci_write,
  37. };
  38. /*
  39. * legacy, numa, and acpi all want to call pcibios_scan_root
  40. * from their initcalls. This flag prevents that.
  41. */
  42. int pcibios_scanned;
  43. /*
  44. * This interrupt-safe spinlock protects all accesses to PCI
  45. * configuration space.
  46. */
  47. DEFINE_SPINLOCK(pci_config_lock);
  48. /*
  49. * Several buggy motherboards address only 16 devices and mirror
  50. * them to next 16 IDs. We try to detect this `feature' on all
  51. * primary buses (those containing host bridges as they are
  52. * expected to be unique) and remove the ghost devices.
  53. */
  54. static void __devinit pcibios_fixup_ghosts(struct pci_bus *b)
  55. {
  56. struct list_head *ln, *mn;
  57. struct pci_dev *d, *e;
  58. int mirror = PCI_DEVFN(16,0);
  59. int seen_host_bridge = 0;
  60. int i;
  61. DBG("PCI: Scanning for ghost devices on bus %d\n", b->number);
  62. list_for_each(ln, &b->devices) {
  63. d = pci_dev_b(ln);
  64. if ((d->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  65. seen_host_bridge++;
  66. for (mn=ln->next; mn != &b->devices; mn=mn->next) {
  67. e = pci_dev_b(mn);
  68. if (e->devfn != d->devfn + mirror ||
  69. e->vendor != d->vendor ||
  70. e->device != d->device ||
  71. e->class != d->class)
  72. continue;
  73. for(i=0; i<PCI_NUM_RESOURCES; i++)
  74. if (e->resource[i].start != d->resource[i].start ||
  75. e->resource[i].end != d->resource[i].end ||
  76. e->resource[i].flags != d->resource[i].flags)
  77. continue;
  78. break;
  79. }
  80. if (mn == &b->devices)
  81. return;
  82. }
  83. if (!seen_host_bridge)
  84. return;
  85. printk(KERN_WARNING "PCI: Ignoring ghost devices on bus %02x\n", b->number);
  86. ln = &b->devices;
  87. while (ln->next != &b->devices) {
  88. d = pci_dev_b(ln->next);
  89. if (d->devfn >= mirror) {
  90. list_del(&d->global_list);
  91. list_del(&d->bus_list);
  92. kfree(d);
  93. } else
  94. ln = ln->next;
  95. }
  96. }
  97. /*
  98. * Called after each bus is probed, but before its children
  99. * are examined.
  100. */
  101. void __devinit pcibios_fixup_bus(struct pci_bus *b)
  102. {
  103. pcibios_fixup_ghosts(b);
  104. pci_read_bridge_bases(b);
  105. }
  106. /*
  107. * Only use DMI information to set this if nothing was passed
  108. * on the kernel command line (which was parsed earlier).
  109. */
  110. static int __devinit set_bf_sort(const struct dmi_system_id *d)
  111. {
  112. if (pci_bf_sort == pci_bf_sort_default) {
  113. pci_bf_sort = pci_dmi_bf;
  114. printk(KERN_INFO "PCI: %s detected, enabling pci=bfsort.\n", d->ident);
  115. }
  116. return 0;
  117. }
  118. /*
  119. * Enable renumbering of PCI bus# ranges to reach all PCI busses (Cardbus)
  120. */
  121. #ifdef __i386__
  122. static int __devinit assign_all_busses(const struct dmi_system_id *d)
  123. {
  124. pci_probe |= PCI_ASSIGN_ALL_BUSSES;
  125. printk(KERN_INFO "%s detected: enabling PCI bus# renumbering"
  126. " (pci=assign-busses)\n", d->ident);
  127. return 0;
  128. }
  129. #endif
  130. static struct dmi_system_id __devinitdata pciprobe_dmi_table[] = {
  131. #ifdef __i386__
  132. /*
  133. * Laptops which need pci=assign-busses to see Cardbus cards
  134. */
  135. {
  136. .callback = assign_all_busses,
  137. .ident = "Samsung X20 Laptop",
  138. .matches = {
  139. DMI_MATCH(DMI_SYS_VENDOR, "Samsung Electronics"),
  140. DMI_MATCH(DMI_PRODUCT_NAME, "SX20S"),
  141. },
  142. },
  143. #endif /* __i386__ */
  144. {
  145. .callback = set_bf_sort,
  146. .ident = "Dell PowerEdge 1950",
  147. .matches = {
  148. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  149. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1950"),
  150. },
  151. },
  152. {
  153. .callback = set_bf_sort,
  154. .ident = "Dell PowerEdge 1955",
  155. .matches = {
  156. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  157. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1955"),
  158. },
  159. },
  160. {
  161. .callback = set_bf_sort,
  162. .ident = "Dell PowerEdge 2900",
  163. .matches = {
  164. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  165. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2900"),
  166. },
  167. },
  168. {
  169. .callback = set_bf_sort,
  170. .ident = "Dell PowerEdge 2950",
  171. .matches = {
  172. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  173. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2950"),
  174. },
  175. },
  176. {
  177. .callback = set_bf_sort,
  178. .ident = "Dell PowerEdge R900",
  179. .matches = {
  180. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  181. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge R900"),
  182. },
  183. },
  184. {
  185. .callback = set_bf_sort,
  186. .ident = "HP ProLiant BL20p G3",
  187. .matches = {
  188. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  189. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G3"),
  190. },
  191. },
  192. {
  193. .callback = set_bf_sort,
  194. .ident = "HP ProLiant BL20p G4",
  195. .matches = {
  196. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  197. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G4"),
  198. },
  199. },
  200. {
  201. .callback = set_bf_sort,
  202. .ident = "HP ProLiant BL30p G1",
  203. .matches = {
  204. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  205. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL30p G1"),
  206. },
  207. },
  208. {
  209. .callback = set_bf_sort,
  210. .ident = "HP ProLiant BL25p G1",
  211. .matches = {
  212. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  213. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL25p G1"),
  214. },
  215. },
  216. {
  217. .callback = set_bf_sort,
  218. .ident = "HP ProLiant BL35p G1",
  219. .matches = {
  220. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  221. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL35p G1"),
  222. },
  223. },
  224. {
  225. .callback = set_bf_sort,
  226. .ident = "HP ProLiant BL45p G1",
  227. .matches = {
  228. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  229. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G1"),
  230. },
  231. },
  232. {
  233. .callback = set_bf_sort,
  234. .ident = "HP ProLiant BL45p G2",
  235. .matches = {
  236. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  237. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G2"),
  238. },
  239. },
  240. {
  241. .callback = set_bf_sort,
  242. .ident = "HP ProLiant BL460c G1",
  243. .matches = {
  244. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  245. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL460c G1"),
  246. },
  247. },
  248. {
  249. .callback = set_bf_sort,
  250. .ident = "HP ProLiant BL465c G1",
  251. .matches = {
  252. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  253. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL465c G1"),
  254. },
  255. },
  256. {
  257. .callback = set_bf_sort,
  258. .ident = "HP ProLiant BL480c G1",
  259. .matches = {
  260. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  261. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL480c G1"),
  262. },
  263. },
  264. {
  265. .callback = set_bf_sort,
  266. .ident = "HP ProLiant BL685c G1",
  267. .matches = {
  268. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  269. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL685c G1"),
  270. },
  271. },
  272. #ifdef __i386__
  273. {
  274. .callback = assign_all_busses,
  275. .ident = "Compaq EVO N800c",
  276. .matches = {
  277. DMI_MATCH(DMI_SYS_VENDOR, "Compaq"),
  278. DMI_MATCH(DMI_PRODUCT_NAME, "EVO N800c"),
  279. },
  280. },
  281. #endif
  282. {}
  283. };
  284. struct pci_bus * __devinit pcibios_scan_root(int busnum)
  285. {
  286. struct pci_bus *bus = NULL;
  287. struct pci_sysdata *sd;
  288. dmi_check_system(pciprobe_dmi_table);
  289. while ((bus = pci_find_next_bus(bus)) != NULL) {
  290. if (bus->number == busnum) {
  291. /* Already scanned */
  292. return bus;
  293. }
  294. }
  295. /* Allocate per-root-bus (not per bus) arch-specific data.
  296. * TODO: leak; this memory is never freed.
  297. * It's arguable whether it's worth the trouble to care.
  298. */
  299. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  300. if (!sd) {
  301. printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
  302. return NULL;
  303. }
  304. printk(KERN_DEBUG "PCI: Probing PCI hardware (bus %02x)\n", busnum);
  305. return pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
  306. }
  307. extern u8 pci_cache_line_size;
  308. static int __init pcibios_init(void)
  309. {
  310. struct cpuinfo_x86 *c = &boot_cpu_data;
  311. if (!raw_pci_ops) {
  312. printk(KERN_WARNING "PCI: System does not support PCI\n");
  313. return 0;
  314. }
  315. /*
  316. * Assume PCI cacheline size of 32 bytes for all x86s except K7/K8
  317. * and P4. It's also good for 386/486s (which actually have 16)
  318. * as quite a few PCI devices do not support smaller values.
  319. */
  320. pci_cache_line_size = 32 >> 2;
  321. if (c->x86 >= 6 && c->x86_vendor == X86_VENDOR_AMD)
  322. pci_cache_line_size = 64 >> 2; /* K7 & K8 */
  323. else if (c->x86 > 6 && c->x86_vendor == X86_VENDOR_INTEL)
  324. pci_cache_line_size = 128 >> 2; /* P4 */
  325. pcibios_resource_survey();
  326. if (pci_bf_sort >= pci_force_bf)
  327. pci_sort_breadthfirst();
  328. #ifdef CONFIG_PCI_BIOS
  329. if ((pci_probe & PCI_BIOS_SORT) && !(pci_probe & PCI_NO_SORT))
  330. pcibios_sort();
  331. #endif
  332. return 0;
  333. }
  334. subsys_initcall(pcibios_init);
  335. char * __devinit pcibios_setup(char *str)
  336. {
  337. if (!strcmp(str, "off")) {
  338. pci_probe = 0;
  339. return NULL;
  340. } else if (!strcmp(str, "bfsort")) {
  341. pci_bf_sort = pci_force_bf;
  342. return NULL;
  343. } else if (!strcmp(str, "nobfsort")) {
  344. pci_bf_sort = pci_force_nobf;
  345. return NULL;
  346. }
  347. #ifdef CONFIG_PCI_BIOS
  348. else if (!strcmp(str, "bios")) {
  349. pci_probe = PCI_PROBE_BIOS;
  350. return NULL;
  351. } else if (!strcmp(str, "nobios")) {
  352. pci_probe &= ~PCI_PROBE_BIOS;
  353. return NULL;
  354. } else if (!strcmp(str, "nosort")) {
  355. pci_probe |= PCI_NO_SORT;
  356. return NULL;
  357. } else if (!strcmp(str, "biosirq")) {
  358. pci_probe |= PCI_BIOS_IRQ_SCAN;
  359. return NULL;
  360. } else if (!strncmp(str, "pirqaddr=", 9)) {
  361. pirq_table_addr = simple_strtoul(str+9, NULL, 0);
  362. return NULL;
  363. }
  364. #endif
  365. #ifdef CONFIG_PCI_DIRECT
  366. else if (!strcmp(str, "conf1")) {
  367. pci_probe = PCI_PROBE_CONF1 | PCI_NO_CHECKS;
  368. return NULL;
  369. }
  370. else if (!strcmp(str, "conf2")) {
  371. pci_probe = PCI_PROBE_CONF2 | PCI_NO_CHECKS;
  372. return NULL;
  373. }
  374. #endif
  375. #ifdef CONFIG_PCI_MMCONFIG
  376. else if (!strcmp(str, "nommconf")) {
  377. pci_probe &= ~PCI_PROBE_MMCONF;
  378. return NULL;
  379. }
  380. #endif
  381. else if (!strcmp(str, "noacpi")) {
  382. acpi_noirq_set();
  383. return NULL;
  384. }
  385. else if (!strcmp(str, "noearly")) {
  386. pci_probe |= PCI_PROBE_NOEARLY;
  387. return NULL;
  388. }
  389. #ifndef CONFIG_X86_VISWS
  390. else if (!strcmp(str, "usepirqmask")) {
  391. pci_probe |= PCI_USE_PIRQ_MASK;
  392. return NULL;
  393. } else if (!strncmp(str, "irqmask=", 8)) {
  394. pcibios_irq_mask = simple_strtol(str+8, NULL, 0);
  395. return NULL;
  396. } else if (!strncmp(str, "lastbus=", 8)) {
  397. pcibios_last_bus = simple_strtol(str+8, NULL, 0);
  398. return NULL;
  399. }
  400. #endif
  401. else if (!strcmp(str, "rom")) {
  402. pci_probe |= PCI_ASSIGN_ROMS;
  403. return NULL;
  404. } else if (!strcmp(str, "assign-busses")) {
  405. pci_probe |= PCI_ASSIGN_ALL_BUSSES;
  406. return NULL;
  407. } else if (!strcmp(str, "use_crs")) {
  408. pci_probe |= PCI_USE__CRS;
  409. return NULL;
  410. } else if (!strcmp(str, "routeirq")) {
  411. pci_routeirq = 1;
  412. return NULL;
  413. }
  414. return str;
  415. }
  416. unsigned int pcibios_assign_all_busses(void)
  417. {
  418. return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
  419. }
  420. int pcibios_enable_device(struct pci_dev *dev, int mask)
  421. {
  422. int err;
  423. if ((err = pcibios_enable_resources(dev, mask)) < 0)
  424. return err;
  425. if (!dev->msi_enabled)
  426. return pcibios_enable_irq(dev);
  427. return 0;
  428. }
  429. void pcibios_disable_device (struct pci_dev *dev)
  430. {
  431. if (!dev->msi_enabled && pcibios_disable_irq)
  432. pcibios_disable_irq(dev);
  433. }
  434. struct pci_bus *pci_scan_bus_with_sysdata(int busno)
  435. {
  436. struct pci_bus *bus = NULL;
  437. struct pci_sysdata *sd;
  438. /*
  439. * Allocate per-root-bus (not per bus) arch-specific data.
  440. * TODO: leak; this memory is never freed.
  441. * It's arguable whether it's worth the trouble to care.
  442. */
  443. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  444. if (!sd) {
  445. printk(KERN_ERR "PCI: OOM, skipping PCI bus %02x\n", busno);
  446. return NULL;
  447. }
  448. sd->node = -1;
  449. bus = pci_scan_bus(busno, &pci_root_ops, sd);
  450. if (!bus)
  451. kfree(sd);
  452. return bus;
  453. }