common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. {
  273. .callback = set_bf_sort,
  274. .ident = "HP ProLiant DL385 G2",
  275. .matches = {
  276. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  277. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL385 G2"),
  278. },
  279. },
  280. {
  281. .callback = set_bf_sort,
  282. .ident = "HP ProLiant DL585 G2",
  283. .matches = {
  284. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  285. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL585 G2"),
  286. },
  287. },
  288. #ifdef __i386__
  289. {
  290. .callback = assign_all_busses,
  291. .ident = "Compaq EVO N800c",
  292. .matches = {
  293. DMI_MATCH(DMI_SYS_VENDOR, "Compaq"),
  294. DMI_MATCH(DMI_PRODUCT_NAME, "EVO N800c"),
  295. },
  296. },
  297. #endif
  298. {}
  299. };
  300. struct pci_bus * __devinit pcibios_scan_root(int busnum)
  301. {
  302. struct pci_bus *bus = NULL;
  303. struct pci_sysdata *sd;
  304. dmi_check_system(pciprobe_dmi_table);
  305. while ((bus = pci_find_next_bus(bus)) != NULL) {
  306. if (bus->number == busnum) {
  307. /* Already scanned */
  308. return bus;
  309. }
  310. }
  311. /* Allocate per-root-bus (not per bus) arch-specific data.
  312. * TODO: leak; this memory is never freed.
  313. * It's arguable whether it's worth the trouble to care.
  314. */
  315. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  316. if (!sd) {
  317. printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
  318. return NULL;
  319. }
  320. printk(KERN_DEBUG "PCI: Probing PCI hardware (bus %02x)\n", busnum);
  321. return pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
  322. }
  323. extern u8 pci_cache_line_size;
  324. static int __init pcibios_init(void)
  325. {
  326. struct cpuinfo_x86 *c = &boot_cpu_data;
  327. if (!raw_pci_ops) {
  328. printk(KERN_WARNING "PCI: System does not support PCI\n");
  329. return 0;
  330. }
  331. /*
  332. * Assume PCI cacheline size of 32 bytes for all x86s except K7/K8
  333. * and P4. It's also good for 386/486s (which actually have 16)
  334. * as quite a few PCI devices do not support smaller values.
  335. */
  336. pci_cache_line_size = 32 >> 2;
  337. if (c->x86 >= 6 && c->x86_vendor == X86_VENDOR_AMD)
  338. pci_cache_line_size = 64 >> 2; /* K7 & K8 */
  339. else if (c->x86 > 6 && c->x86_vendor == X86_VENDOR_INTEL)
  340. pci_cache_line_size = 128 >> 2; /* P4 */
  341. pcibios_resource_survey();
  342. if (pci_bf_sort >= pci_force_bf)
  343. pci_sort_breadthfirst();
  344. #ifdef CONFIG_PCI_BIOS
  345. if ((pci_probe & PCI_BIOS_SORT) && !(pci_probe & PCI_NO_SORT))
  346. pcibios_sort();
  347. #endif
  348. return 0;
  349. }
  350. subsys_initcall(pcibios_init);
  351. char * __devinit pcibios_setup(char *str)
  352. {
  353. if (!strcmp(str, "off")) {
  354. pci_probe = 0;
  355. return NULL;
  356. } else if (!strcmp(str, "bfsort")) {
  357. pci_bf_sort = pci_force_bf;
  358. return NULL;
  359. } else if (!strcmp(str, "nobfsort")) {
  360. pci_bf_sort = pci_force_nobf;
  361. return NULL;
  362. }
  363. #ifdef CONFIG_PCI_BIOS
  364. else if (!strcmp(str, "bios")) {
  365. pci_probe = PCI_PROBE_BIOS;
  366. return NULL;
  367. } else if (!strcmp(str, "nobios")) {
  368. pci_probe &= ~PCI_PROBE_BIOS;
  369. return NULL;
  370. } else if (!strcmp(str, "nosort")) {
  371. pci_probe |= PCI_NO_SORT;
  372. return NULL;
  373. } else if (!strcmp(str, "biosirq")) {
  374. pci_probe |= PCI_BIOS_IRQ_SCAN;
  375. return NULL;
  376. } else if (!strncmp(str, "pirqaddr=", 9)) {
  377. pirq_table_addr = simple_strtoul(str+9, NULL, 0);
  378. return NULL;
  379. }
  380. #endif
  381. #ifdef CONFIG_PCI_DIRECT
  382. else if (!strcmp(str, "conf1")) {
  383. pci_probe = PCI_PROBE_CONF1 | PCI_NO_CHECKS;
  384. return NULL;
  385. }
  386. else if (!strcmp(str, "conf2")) {
  387. pci_probe = PCI_PROBE_CONF2 | PCI_NO_CHECKS;
  388. return NULL;
  389. }
  390. #endif
  391. #ifdef CONFIG_PCI_MMCONFIG
  392. else if (!strcmp(str, "nommconf")) {
  393. pci_probe &= ~PCI_PROBE_MMCONF;
  394. return NULL;
  395. }
  396. #endif
  397. else if (!strcmp(str, "noacpi")) {
  398. acpi_noirq_set();
  399. return NULL;
  400. }
  401. else if (!strcmp(str, "noearly")) {
  402. pci_probe |= PCI_PROBE_NOEARLY;
  403. return NULL;
  404. }
  405. #ifndef CONFIG_X86_VISWS
  406. else if (!strcmp(str, "usepirqmask")) {
  407. pci_probe |= PCI_USE_PIRQ_MASK;
  408. return NULL;
  409. } else if (!strncmp(str, "irqmask=", 8)) {
  410. pcibios_irq_mask = simple_strtol(str+8, NULL, 0);
  411. return NULL;
  412. } else if (!strncmp(str, "lastbus=", 8)) {
  413. pcibios_last_bus = simple_strtol(str+8, NULL, 0);
  414. return NULL;
  415. }
  416. #endif
  417. else if (!strcmp(str, "rom")) {
  418. pci_probe |= PCI_ASSIGN_ROMS;
  419. return NULL;
  420. } else if (!strcmp(str, "assign-busses")) {
  421. pci_probe |= PCI_ASSIGN_ALL_BUSSES;
  422. return NULL;
  423. } else if (!strcmp(str, "use_crs")) {
  424. pci_probe |= PCI_USE__CRS;
  425. return NULL;
  426. } else if (!strcmp(str, "routeirq")) {
  427. pci_routeirq = 1;
  428. return NULL;
  429. }
  430. return str;
  431. }
  432. unsigned int pcibios_assign_all_busses(void)
  433. {
  434. return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
  435. }
  436. int pcibios_enable_device(struct pci_dev *dev, int mask)
  437. {
  438. int err;
  439. if ((err = pcibios_enable_resources(dev, mask)) < 0)
  440. return err;
  441. if (!dev->msi_enabled)
  442. return pcibios_enable_irq(dev);
  443. return 0;
  444. }
  445. void pcibios_disable_device (struct pci_dev *dev)
  446. {
  447. if (!dev->msi_enabled && pcibios_disable_irq)
  448. pcibios_disable_irq(dev);
  449. }
  450. struct pci_bus *pci_scan_bus_with_sysdata(int busno)
  451. {
  452. struct pci_bus *bus = NULL;
  453. struct pci_sysdata *sd;
  454. /*
  455. * Allocate per-root-bus (not per bus) arch-specific data.
  456. * TODO: leak; this memory is never freed.
  457. * It's arguable whether it's worth the trouble to care.
  458. */
  459. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  460. if (!sd) {
  461. printk(KERN_ERR "PCI: OOM, skipping PCI bus %02x\n", busno);
  462. return NULL;
  463. }
  464. sd->node = -1;
  465. bus = pci_scan_bus(busno, &pci_root_ops, sd);
  466. if (!bus)
  467. kfree(sd);
  468. return bus;
  469. }