common.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. struct pci_raw_ops *raw_pci_ext_ops;
  25. int raw_pci_read(unsigned int domain, unsigned int bus, unsigned int devfn,
  26. int reg, int len, u32 *val)
  27. {
  28. if (reg < 256 && raw_pci_ops)
  29. return raw_pci_ops->read(domain, bus, devfn, reg, len, val);
  30. if (raw_pci_ext_ops)
  31. return raw_pci_ext_ops->read(domain, bus, devfn, reg, len, val);
  32. return -EINVAL;
  33. }
  34. int raw_pci_write(unsigned int domain, unsigned int bus, unsigned int devfn,
  35. int reg, int len, u32 val)
  36. {
  37. if (reg < 256 && raw_pci_ops)
  38. return raw_pci_ops->write(domain, bus, devfn, reg, len, val);
  39. if (raw_pci_ext_ops)
  40. return raw_pci_ext_ops->write(domain, bus, devfn, reg, len, val);
  41. return -EINVAL;
  42. }
  43. static int pci_read(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
  44. {
  45. return raw_pci_read(pci_domain_nr(bus), bus->number,
  46. devfn, where, size, value);
  47. }
  48. static int pci_write(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
  49. {
  50. return raw_pci_write(pci_domain_nr(bus), bus->number,
  51. devfn, where, size, value);
  52. }
  53. struct pci_ops pci_root_ops = {
  54. .read = pci_read,
  55. .write = pci_write,
  56. };
  57. /*
  58. * legacy, numa, and acpi all want to call pcibios_scan_root
  59. * from their initcalls. This flag prevents that.
  60. */
  61. int pcibios_scanned;
  62. /*
  63. * This interrupt-safe spinlock protects all accesses to PCI
  64. * configuration space.
  65. */
  66. DEFINE_SPINLOCK(pci_config_lock);
  67. static void __devinit pcibios_fixup_device_resources(struct pci_dev *dev)
  68. {
  69. struct resource *rom_r = &dev->resource[PCI_ROM_RESOURCE];
  70. if (rom_r->parent)
  71. return;
  72. if (rom_r->start)
  73. /* we deal with BIOS assigned ROM later */
  74. return;
  75. if (!(pci_probe & PCI_ASSIGN_ROMS))
  76. rom_r->start = rom_r->end = rom_r->flags = 0;
  77. }
  78. static int __devinit can_skip_ioresource_align(const struct dmi_system_id *d)
  79. {
  80. pci_probe |= PCI_CAN_SKIP_ISA_ALIGN;
  81. printk(KERN_INFO "PCI: %s detected, can skip ISA alignment\n", d->ident);
  82. return 0;
  83. }
  84. static struct dmi_system_id can_skip_pciprobe_dmi_table[] __devinitdata = {
  85. /*
  86. * Systems where PCI IO resource ISA alignment can be skipped
  87. * when the ISA enable bit in the bridge control is not set
  88. */
  89. {
  90. .callback = can_skip_ioresource_align,
  91. .ident = "IBM System x3800",
  92. .matches = {
  93. DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
  94. DMI_MATCH(DMI_PRODUCT_NAME, "x3800"),
  95. },
  96. },
  97. {
  98. .callback = can_skip_ioresource_align,
  99. .ident = "IBM System x3850",
  100. .matches = {
  101. DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
  102. DMI_MATCH(DMI_PRODUCT_NAME, "x3850"),
  103. },
  104. },
  105. {
  106. .callback = can_skip_ioresource_align,
  107. .ident = "IBM System x3950",
  108. .matches = {
  109. DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
  110. DMI_MATCH(DMI_PRODUCT_NAME, "x3950"),
  111. },
  112. },
  113. {}
  114. };
  115. void __init dmi_check_skip_isa_align(void)
  116. {
  117. dmi_check_system(can_skip_pciprobe_dmi_table);
  118. }
  119. /*
  120. * Called after each bus is probed, but before its children
  121. * are examined.
  122. */
  123. void __devinit pcibios_fixup_bus(struct pci_bus *b)
  124. {
  125. struct pci_dev *dev;
  126. pci_read_bridge_bases(b);
  127. list_for_each_entry(dev, &b->devices, bus_list)
  128. pcibios_fixup_device_resources(dev);
  129. }
  130. /*
  131. * Only use DMI information to set this if nothing was passed
  132. * on the kernel command line (which was parsed earlier).
  133. */
  134. static int __devinit set_bf_sort(const struct dmi_system_id *d)
  135. {
  136. if (pci_bf_sort == pci_bf_sort_default) {
  137. pci_bf_sort = pci_dmi_bf;
  138. printk(KERN_INFO "PCI: %s detected, enabling pci=bfsort.\n", d->ident);
  139. }
  140. return 0;
  141. }
  142. /*
  143. * Enable renumbering of PCI bus# ranges to reach all PCI busses (Cardbus)
  144. */
  145. #ifdef __i386__
  146. static int __devinit assign_all_busses(const struct dmi_system_id *d)
  147. {
  148. pci_probe |= PCI_ASSIGN_ALL_BUSSES;
  149. printk(KERN_INFO "%s detected: enabling PCI bus# renumbering"
  150. " (pci=assign-busses)\n", d->ident);
  151. return 0;
  152. }
  153. #endif
  154. static struct dmi_system_id __devinitdata pciprobe_dmi_table[] = {
  155. #ifdef __i386__
  156. /*
  157. * Laptops which need pci=assign-busses to see Cardbus cards
  158. */
  159. {
  160. .callback = assign_all_busses,
  161. .ident = "Samsung X20 Laptop",
  162. .matches = {
  163. DMI_MATCH(DMI_SYS_VENDOR, "Samsung Electronics"),
  164. DMI_MATCH(DMI_PRODUCT_NAME, "SX20S"),
  165. },
  166. },
  167. #endif /* __i386__ */
  168. {
  169. .callback = set_bf_sort,
  170. .ident = "Dell PowerEdge 1950",
  171. .matches = {
  172. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  173. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1950"),
  174. },
  175. },
  176. {
  177. .callback = set_bf_sort,
  178. .ident = "Dell PowerEdge 1955",
  179. .matches = {
  180. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  181. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1955"),
  182. },
  183. },
  184. {
  185. .callback = set_bf_sort,
  186. .ident = "Dell PowerEdge 2900",
  187. .matches = {
  188. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  189. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2900"),
  190. },
  191. },
  192. {
  193. .callback = set_bf_sort,
  194. .ident = "Dell PowerEdge 2950",
  195. .matches = {
  196. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  197. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2950"),
  198. },
  199. },
  200. {
  201. .callback = set_bf_sort,
  202. .ident = "Dell PowerEdge R900",
  203. .matches = {
  204. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  205. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge R900"),
  206. },
  207. },
  208. {
  209. .callback = set_bf_sort,
  210. .ident = "HP ProLiant BL20p G3",
  211. .matches = {
  212. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  213. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G3"),
  214. },
  215. },
  216. {
  217. .callback = set_bf_sort,
  218. .ident = "HP ProLiant BL20p G4",
  219. .matches = {
  220. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  221. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G4"),
  222. },
  223. },
  224. {
  225. .callback = set_bf_sort,
  226. .ident = "HP ProLiant BL30p G1",
  227. .matches = {
  228. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  229. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL30p G1"),
  230. },
  231. },
  232. {
  233. .callback = set_bf_sort,
  234. .ident = "HP ProLiant BL25p G1",
  235. .matches = {
  236. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  237. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL25p G1"),
  238. },
  239. },
  240. {
  241. .callback = set_bf_sort,
  242. .ident = "HP ProLiant BL35p G1",
  243. .matches = {
  244. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  245. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL35p G1"),
  246. },
  247. },
  248. {
  249. .callback = set_bf_sort,
  250. .ident = "HP ProLiant BL45p G1",
  251. .matches = {
  252. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  253. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G1"),
  254. },
  255. },
  256. {
  257. .callback = set_bf_sort,
  258. .ident = "HP ProLiant BL45p G2",
  259. .matches = {
  260. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  261. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G2"),
  262. },
  263. },
  264. {
  265. .callback = set_bf_sort,
  266. .ident = "HP ProLiant BL460c G1",
  267. .matches = {
  268. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  269. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL460c G1"),
  270. },
  271. },
  272. {
  273. .callback = set_bf_sort,
  274. .ident = "HP ProLiant BL465c G1",
  275. .matches = {
  276. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  277. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL465c G1"),
  278. },
  279. },
  280. {
  281. .callback = set_bf_sort,
  282. .ident = "HP ProLiant BL480c G1",
  283. .matches = {
  284. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  285. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL480c G1"),
  286. },
  287. },
  288. {
  289. .callback = set_bf_sort,
  290. .ident = "HP ProLiant BL685c G1",
  291. .matches = {
  292. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  293. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL685c G1"),
  294. },
  295. },
  296. {
  297. .callback = set_bf_sort,
  298. .ident = "HP ProLiant DL385 G2",
  299. .matches = {
  300. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  301. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL385 G2"),
  302. },
  303. },
  304. {
  305. .callback = set_bf_sort,
  306. .ident = "HP ProLiant DL585 G2",
  307. .matches = {
  308. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  309. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL585 G2"),
  310. },
  311. },
  312. #ifdef __i386__
  313. {
  314. .callback = assign_all_busses,
  315. .ident = "Compaq EVO N800c",
  316. .matches = {
  317. DMI_MATCH(DMI_SYS_VENDOR, "Compaq"),
  318. DMI_MATCH(DMI_PRODUCT_NAME, "EVO N800c"),
  319. },
  320. },
  321. #endif
  322. {
  323. .callback = set_bf_sort,
  324. .ident = "HP ProLiant DL385 G2",
  325. .matches = {
  326. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  327. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL385 G2"),
  328. },
  329. },
  330. {
  331. .callback = set_bf_sort,
  332. .ident = "HP ProLiant DL585 G2",
  333. .matches = {
  334. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  335. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL585 G2"),
  336. },
  337. },
  338. {}
  339. };
  340. void __init dmi_check_pciprobe(void)
  341. {
  342. dmi_check_system(pciprobe_dmi_table);
  343. }
  344. struct pci_bus * __devinit pcibios_scan_root(int busnum)
  345. {
  346. struct pci_bus *bus = NULL;
  347. struct pci_sysdata *sd;
  348. while ((bus = pci_find_next_bus(bus)) != NULL) {
  349. if (bus->number == busnum) {
  350. /* Already scanned */
  351. return bus;
  352. }
  353. }
  354. /* Allocate per-root-bus (not per bus) arch-specific data.
  355. * TODO: leak; this memory is never freed.
  356. * It's arguable whether it's worth the trouble to care.
  357. */
  358. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  359. if (!sd) {
  360. printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
  361. return NULL;
  362. }
  363. sd->node = get_mp_bus_to_node(busnum);
  364. printk(KERN_DEBUG "PCI: Probing PCI hardware (bus %02x)\n", busnum);
  365. bus = pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
  366. if (!bus)
  367. kfree(sd);
  368. return bus;
  369. }
  370. extern u8 pci_cache_line_size;
  371. static int __init pcibios_init(void)
  372. {
  373. struct cpuinfo_x86 *c = &boot_cpu_data;
  374. if (!raw_pci_ops) {
  375. printk(KERN_WARNING "PCI: System does not support PCI\n");
  376. return 0;
  377. }
  378. /*
  379. * Assume PCI cacheline size of 32 bytes for all x86s except K7/K8
  380. * and P4. It's also good for 386/486s (which actually have 16)
  381. * as quite a few PCI devices do not support smaller values.
  382. */
  383. pci_cache_line_size = 32 >> 2;
  384. if (c->x86 >= 6 && c->x86_vendor == X86_VENDOR_AMD)
  385. pci_cache_line_size = 64 >> 2; /* K7 & K8 */
  386. else if (c->x86 > 6 && c->x86_vendor == X86_VENDOR_INTEL)
  387. pci_cache_line_size = 128 >> 2; /* P4 */
  388. pcibios_resource_survey();
  389. if (pci_bf_sort >= pci_force_bf)
  390. pci_sort_breadthfirst();
  391. return 0;
  392. }
  393. subsys_initcall(pcibios_init);
  394. char * __devinit pcibios_setup(char *str)
  395. {
  396. if (!strcmp(str, "off")) {
  397. pci_probe = 0;
  398. return NULL;
  399. } else if (!strcmp(str, "bfsort")) {
  400. pci_bf_sort = pci_force_bf;
  401. return NULL;
  402. } else if (!strcmp(str, "nobfsort")) {
  403. pci_bf_sort = pci_force_nobf;
  404. return NULL;
  405. }
  406. #ifdef CONFIG_PCI_BIOS
  407. else if (!strcmp(str, "bios")) {
  408. pci_probe = PCI_PROBE_BIOS;
  409. return NULL;
  410. } else if (!strcmp(str, "nobios")) {
  411. pci_probe &= ~PCI_PROBE_BIOS;
  412. return NULL;
  413. } else if (!strcmp(str, "biosirq")) {
  414. pci_probe |= PCI_BIOS_IRQ_SCAN;
  415. return NULL;
  416. } else if (!strncmp(str, "pirqaddr=", 9)) {
  417. pirq_table_addr = simple_strtoul(str+9, NULL, 0);
  418. return NULL;
  419. }
  420. #endif
  421. #ifdef CONFIG_PCI_DIRECT
  422. else if (!strcmp(str, "conf1")) {
  423. pci_probe = PCI_PROBE_CONF1 | PCI_NO_CHECKS;
  424. return NULL;
  425. }
  426. else if (!strcmp(str, "conf2")) {
  427. pci_probe = PCI_PROBE_CONF2 | PCI_NO_CHECKS;
  428. return NULL;
  429. }
  430. #endif
  431. #ifdef CONFIG_PCI_MMCONFIG
  432. else if (!strcmp(str, "nommconf")) {
  433. pci_probe &= ~PCI_PROBE_MMCONF;
  434. return NULL;
  435. }
  436. else if (!strcmp(str, "check_enable_amd_mmconf")) {
  437. pci_probe |= PCI_CHECK_ENABLE_AMD_MMCONF;
  438. return NULL;
  439. }
  440. #endif
  441. else if (!strcmp(str, "noacpi")) {
  442. acpi_noirq_set();
  443. return NULL;
  444. }
  445. else if (!strcmp(str, "noearly")) {
  446. pci_probe |= PCI_PROBE_NOEARLY;
  447. return NULL;
  448. }
  449. #ifndef CONFIG_X86_VISWS
  450. else if (!strcmp(str, "usepirqmask")) {
  451. pci_probe |= PCI_USE_PIRQ_MASK;
  452. return NULL;
  453. } else if (!strncmp(str, "irqmask=", 8)) {
  454. pcibios_irq_mask = simple_strtol(str+8, NULL, 0);
  455. return NULL;
  456. } else if (!strncmp(str, "lastbus=", 8)) {
  457. pcibios_last_bus = simple_strtol(str+8, NULL, 0);
  458. return NULL;
  459. }
  460. #endif
  461. else if (!strcmp(str, "rom")) {
  462. pci_probe |= PCI_ASSIGN_ROMS;
  463. return NULL;
  464. } else if (!strcmp(str, "assign-busses")) {
  465. pci_probe |= PCI_ASSIGN_ALL_BUSSES;
  466. return NULL;
  467. } else if (!strcmp(str, "use_crs")) {
  468. pci_probe |= PCI_USE__CRS;
  469. return NULL;
  470. } else if (!strcmp(str, "routeirq")) {
  471. pci_routeirq = 1;
  472. return NULL;
  473. } else if (!strcmp(str, "skip_isa_align")) {
  474. pci_probe |= PCI_CAN_SKIP_ISA_ALIGN;
  475. return NULL;
  476. }
  477. return str;
  478. }
  479. unsigned int pcibios_assign_all_busses(void)
  480. {
  481. return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
  482. }
  483. int pcibios_enable_device(struct pci_dev *dev, int mask)
  484. {
  485. int err;
  486. if ((err = pci_enable_resources(dev, mask)) < 0)
  487. return err;
  488. if (!dev->msi_enabled)
  489. return pcibios_enable_irq(dev);
  490. return 0;
  491. }
  492. void pcibios_disable_device (struct pci_dev *dev)
  493. {
  494. if (!dev->msi_enabled && pcibios_disable_irq)
  495. pcibios_disable_irq(dev);
  496. }
  497. struct pci_bus * __devinit pci_scan_bus_on_node(int busno, struct pci_ops *ops, int node)
  498. {
  499. struct pci_bus *bus = NULL;
  500. struct pci_sysdata *sd;
  501. /*
  502. * Allocate per-root-bus (not per bus) arch-specific data.
  503. * TODO: leak; this memory is never freed.
  504. * It's arguable whether it's worth the trouble to care.
  505. */
  506. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  507. if (!sd) {
  508. printk(KERN_ERR "PCI: OOM, skipping PCI bus %02x\n", busno);
  509. return NULL;
  510. }
  511. sd->node = node;
  512. bus = pci_scan_bus(busno, ops, sd);
  513. if (!bus)
  514. kfree(sd);
  515. return bus;
  516. }
  517. struct pci_bus * __devinit pci_scan_bus_with_sysdata(int busno)
  518. {
  519. return pci_scan_bus_on_node(busno, &pci_root_ops, -1);
  520. }