common.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. /*
  79. * Called after each bus is probed, but before its children
  80. * are examined.
  81. */
  82. void __devinit pcibios_fixup_bus(struct pci_bus *b)
  83. {
  84. struct pci_dev *dev;
  85. pci_read_bridge_bases(b);
  86. list_for_each_entry(dev, &b->devices, bus_list)
  87. pcibios_fixup_device_resources(dev);
  88. }
  89. /*
  90. * Only use DMI information to set this if nothing was passed
  91. * on the kernel command line (which was parsed earlier).
  92. */
  93. static int __devinit set_bf_sort(const struct dmi_system_id *d)
  94. {
  95. if (pci_bf_sort == pci_bf_sort_default) {
  96. pci_bf_sort = pci_dmi_bf;
  97. printk(KERN_INFO "PCI: %s detected, enabling pci=bfsort.\n", d->ident);
  98. }
  99. return 0;
  100. }
  101. /*
  102. * Enable renumbering of PCI bus# ranges to reach all PCI busses (Cardbus)
  103. */
  104. #ifdef __i386__
  105. static int __devinit assign_all_busses(const struct dmi_system_id *d)
  106. {
  107. pci_probe |= PCI_ASSIGN_ALL_BUSSES;
  108. printk(KERN_INFO "%s detected: enabling PCI bus# renumbering"
  109. " (pci=assign-busses)\n", d->ident);
  110. return 0;
  111. }
  112. #endif
  113. static struct dmi_system_id __devinitdata pciprobe_dmi_table[] = {
  114. #ifdef __i386__
  115. /*
  116. * Laptops which need pci=assign-busses to see Cardbus cards
  117. */
  118. {
  119. .callback = assign_all_busses,
  120. .ident = "Samsung X20 Laptop",
  121. .matches = {
  122. DMI_MATCH(DMI_SYS_VENDOR, "Samsung Electronics"),
  123. DMI_MATCH(DMI_PRODUCT_NAME, "SX20S"),
  124. },
  125. },
  126. #endif /* __i386__ */
  127. {
  128. .callback = set_bf_sort,
  129. .ident = "Dell PowerEdge 1950",
  130. .matches = {
  131. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  132. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1950"),
  133. },
  134. },
  135. {
  136. .callback = set_bf_sort,
  137. .ident = "Dell PowerEdge 1955",
  138. .matches = {
  139. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  140. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1955"),
  141. },
  142. },
  143. {
  144. .callback = set_bf_sort,
  145. .ident = "Dell PowerEdge 2900",
  146. .matches = {
  147. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  148. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2900"),
  149. },
  150. },
  151. {
  152. .callback = set_bf_sort,
  153. .ident = "Dell PowerEdge 2950",
  154. .matches = {
  155. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  156. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2950"),
  157. },
  158. },
  159. {
  160. .callback = set_bf_sort,
  161. .ident = "Dell PowerEdge R900",
  162. .matches = {
  163. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  164. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge R900"),
  165. },
  166. },
  167. {
  168. .callback = set_bf_sort,
  169. .ident = "HP ProLiant BL20p G3",
  170. .matches = {
  171. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  172. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G3"),
  173. },
  174. },
  175. {
  176. .callback = set_bf_sort,
  177. .ident = "HP ProLiant BL20p G4",
  178. .matches = {
  179. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  180. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL20p G4"),
  181. },
  182. },
  183. {
  184. .callback = set_bf_sort,
  185. .ident = "HP ProLiant BL30p G1",
  186. .matches = {
  187. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  188. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL30p G1"),
  189. },
  190. },
  191. {
  192. .callback = set_bf_sort,
  193. .ident = "HP ProLiant BL25p G1",
  194. .matches = {
  195. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  196. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL25p G1"),
  197. },
  198. },
  199. {
  200. .callback = set_bf_sort,
  201. .ident = "HP ProLiant BL35p G1",
  202. .matches = {
  203. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  204. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL35p G1"),
  205. },
  206. },
  207. {
  208. .callback = set_bf_sort,
  209. .ident = "HP ProLiant BL45p G1",
  210. .matches = {
  211. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  212. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G1"),
  213. },
  214. },
  215. {
  216. .callback = set_bf_sort,
  217. .ident = "HP ProLiant BL45p G2",
  218. .matches = {
  219. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  220. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL45p G2"),
  221. },
  222. },
  223. {
  224. .callback = set_bf_sort,
  225. .ident = "HP ProLiant BL460c G1",
  226. .matches = {
  227. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  228. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL460c G1"),
  229. },
  230. },
  231. {
  232. .callback = set_bf_sort,
  233. .ident = "HP ProLiant BL465c G1",
  234. .matches = {
  235. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  236. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL465c G1"),
  237. },
  238. },
  239. {
  240. .callback = set_bf_sort,
  241. .ident = "HP ProLiant BL480c G1",
  242. .matches = {
  243. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  244. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL480c G1"),
  245. },
  246. },
  247. {
  248. .callback = set_bf_sort,
  249. .ident = "HP ProLiant BL685c G1",
  250. .matches = {
  251. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  252. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant BL685c G1"),
  253. },
  254. },
  255. {
  256. .callback = set_bf_sort,
  257. .ident = "HP ProLiant DL385 G2",
  258. .matches = {
  259. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  260. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL385 G2"),
  261. },
  262. },
  263. {
  264. .callback = set_bf_sort,
  265. .ident = "HP ProLiant DL585 G2",
  266. .matches = {
  267. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  268. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL585 G2"),
  269. },
  270. },
  271. #ifdef __i386__
  272. {
  273. .callback = assign_all_busses,
  274. .ident = "Compaq EVO N800c",
  275. .matches = {
  276. DMI_MATCH(DMI_SYS_VENDOR, "Compaq"),
  277. DMI_MATCH(DMI_PRODUCT_NAME, "EVO N800c"),
  278. },
  279. },
  280. #endif
  281. {
  282. .callback = set_bf_sort,
  283. .ident = "HP ProLiant DL385 G2",
  284. .matches = {
  285. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  286. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL385 G2"),
  287. },
  288. },
  289. {
  290. .callback = set_bf_sort,
  291. .ident = "HP ProLiant DL585 G2",
  292. .matches = {
  293. DMI_MATCH(DMI_SYS_VENDOR, "HP"),
  294. DMI_MATCH(DMI_PRODUCT_NAME, "ProLiant DL585 G2"),
  295. },
  296. },
  297. {}
  298. };
  299. struct pci_bus * __devinit pcibios_scan_root(int busnum)
  300. {
  301. struct pci_bus *bus = NULL;
  302. struct pci_sysdata *sd;
  303. dmi_check_system(pciprobe_dmi_table);
  304. while ((bus = pci_find_next_bus(bus)) != NULL) {
  305. if (bus->number == busnum) {
  306. /* Already scanned */
  307. return bus;
  308. }
  309. }
  310. /* Allocate per-root-bus (not per bus) arch-specific data.
  311. * TODO: leak; this memory is never freed.
  312. * It's arguable whether it's worth the trouble to care.
  313. */
  314. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  315. if (!sd) {
  316. printk(KERN_ERR "PCI: OOM, not probing PCI bus %02x\n", busnum);
  317. return NULL;
  318. }
  319. sd->node = get_mp_bus_to_node(busnum);
  320. printk(KERN_DEBUG "PCI: Probing PCI hardware (bus %02x)\n", busnum);
  321. bus = pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
  322. if (!bus)
  323. kfree(sd);
  324. return bus;
  325. }
  326. extern u8 pci_cache_line_size;
  327. static int __init pcibios_init(void)
  328. {
  329. struct cpuinfo_x86 *c = &boot_cpu_data;
  330. if (!raw_pci_ops) {
  331. printk(KERN_WARNING "PCI: System does not support PCI\n");
  332. return 0;
  333. }
  334. /*
  335. * Assume PCI cacheline size of 32 bytes for all x86s except K7/K8
  336. * and P4. It's also good for 386/486s (which actually have 16)
  337. * as quite a few PCI devices do not support smaller values.
  338. */
  339. pci_cache_line_size = 32 >> 2;
  340. if (c->x86 >= 6 && c->x86_vendor == X86_VENDOR_AMD)
  341. pci_cache_line_size = 64 >> 2; /* K7 & K8 */
  342. else if (c->x86 > 6 && c->x86_vendor == X86_VENDOR_INTEL)
  343. pci_cache_line_size = 128 >> 2; /* P4 */
  344. pcibios_resource_survey();
  345. if (pci_bf_sort >= pci_force_bf)
  346. pci_sort_breadthfirst();
  347. return 0;
  348. }
  349. subsys_initcall(pcibios_init);
  350. char * __devinit pcibios_setup(char *str)
  351. {
  352. if (!strcmp(str, "off")) {
  353. pci_probe = 0;
  354. return NULL;
  355. } else if (!strcmp(str, "bfsort")) {
  356. pci_bf_sort = pci_force_bf;
  357. return NULL;
  358. } else if (!strcmp(str, "nobfsort")) {
  359. pci_bf_sort = pci_force_nobf;
  360. return NULL;
  361. }
  362. #ifdef CONFIG_PCI_BIOS
  363. else if (!strcmp(str, "bios")) {
  364. pci_probe = PCI_PROBE_BIOS;
  365. return NULL;
  366. } else if (!strcmp(str, "nobios")) {
  367. pci_probe &= ~PCI_PROBE_BIOS;
  368. return NULL;
  369. } else if (!strcmp(str, "biosirq")) {
  370. pci_probe |= PCI_BIOS_IRQ_SCAN;
  371. return NULL;
  372. } else if (!strncmp(str, "pirqaddr=", 9)) {
  373. pirq_table_addr = simple_strtoul(str+9, NULL, 0);
  374. return NULL;
  375. }
  376. #endif
  377. #ifdef CONFIG_PCI_DIRECT
  378. else if (!strcmp(str, "conf1")) {
  379. pci_probe = PCI_PROBE_CONF1 | PCI_NO_CHECKS;
  380. return NULL;
  381. }
  382. else if (!strcmp(str, "conf2")) {
  383. pci_probe = PCI_PROBE_CONF2 | PCI_NO_CHECKS;
  384. return NULL;
  385. }
  386. #endif
  387. #ifdef CONFIG_PCI_MMCONFIG
  388. else if (!strcmp(str, "nommconf")) {
  389. pci_probe &= ~PCI_PROBE_MMCONF;
  390. return NULL;
  391. }
  392. else if (!strcmp(str, "check_enable_amd_mmconf")) {
  393. pci_probe |= PCI_CHECK_ENABLE_AMD_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 = pci_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_on_node(int busno, struct pci_ops *ops, int node)
  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 = node;
  465. bus = pci_scan_bus(busno, ops, sd);
  466. if (!bus)
  467. kfree(sd);
  468. return bus;
  469. }
  470. struct pci_bus *pci_scan_bus_with_sysdata(int busno)
  471. {
  472. return pci_scan_bus_on_node(busno, &pci_root_ops, -1);
  473. }