common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. printk(KERN_DEBUG "PCI: Probing PCI hardware (bus %02x)\n", busnum);
  320. return pci_scan_bus_parented(NULL, busnum, &pci_root_ops, sd);
  321. }
  322. extern u8 pci_cache_line_size;
  323. static int __init pcibios_init(void)
  324. {
  325. struct cpuinfo_x86 *c = &boot_cpu_data;
  326. if (!raw_pci_ops) {
  327. printk(KERN_WARNING "PCI: System does not support PCI\n");
  328. return 0;
  329. }
  330. /*
  331. * Assume PCI cacheline size of 32 bytes for all x86s except K7/K8
  332. * and P4. It's also good for 386/486s (which actually have 16)
  333. * as quite a few PCI devices do not support smaller values.
  334. */
  335. pci_cache_line_size = 32 >> 2;
  336. if (c->x86 >= 6 && c->x86_vendor == X86_VENDOR_AMD)
  337. pci_cache_line_size = 64 >> 2; /* K7 & K8 */
  338. else if (c->x86 > 6 && c->x86_vendor == X86_VENDOR_INTEL)
  339. pci_cache_line_size = 128 >> 2; /* P4 */
  340. pcibios_resource_survey();
  341. if (pci_bf_sort >= pci_force_bf)
  342. pci_sort_breadthfirst();
  343. return 0;
  344. }
  345. subsys_initcall(pcibios_init);
  346. char * __devinit pcibios_setup(char *str)
  347. {
  348. if (!strcmp(str, "off")) {
  349. pci_probe = 0;
  350. return NULL;
  351. } else if (!strcmp(str, "bfsort")) {
  352. pci_bf_sort = pci_force_bf;
  353. return NULL;
  354. } else if (!strcmp(str, "nobfsort")) {
  355. pci_bf_sort = pci_force_nobf;
  356. return NULL;
  357. }
  358. #ifdef CONFIG_PCI_BIOS
  359. else if (!strcmp(str, "bios")) {
  360. pci_probe = PCI_PROBE_BIOS;
  361. return NULL;
  362. } else if (!strcmp(str, "nobios")) {
  363. pci_probe &= ~PCI_PROBE_BIOS;
  364. return NULL;
  365. } else if (!strcmp(str, "biosirq")) {
  366. pci_probe |= PCI_BIOS_IRQ_SCAN;
  367. return NULL;
  368. } else if (!strncmp(str, "pirqaddr=", 9)) {
  369. pirq_table_addr = simple_strtoul(str+9, NULL, 0);
  370. return NULL;
  371. }
  372. #endif
  373. #ifdef CONFIG_PCI_DIRECT
  374. else if (!strcmp(str, "conf1")) {
  375. pci_probe = PCI_PROBE_CONF1 | PCI_NO_CHECKS;
  376. return NULL;
  377. }
  378. else if (!strcmp(str, "conf2")) {
  379. pci_probe = PCI_PROBE_CONF2 | PCI_NO_CHECKS;
  380. return NULL;
  381. }
  382. #endif
  383. #ifdef CONFIG_PCI_MMCONFIG
  384. else if (!strcmp(str, "nommconf")) {
  385. pci_probe &= ~PCI_PROBE_MMCONF;
  386. return NULL;
  387. }
  388. #endif
  389. else if (!strcmp(str, "noacpi")) {
  390. acpi_noirq_set();
  391. return NULL;
  392. }
  393. else if (!strcmp(str, "noearly")) {
  394. pci_probe |= PCI_PROBE_NOEARLY;
  395. return NULL;
  396. }
  397. #ifndef CONFIG_X86_VISWS
  398. else if (!strcmp(str, "usepirqmask")) {
  399. pci_probe |= PCI_USE_PIRQ_MASK;
  400. return NULL;
  401. } else if (!strncmp(str, "irqmask=", 8)) {
  402. pcibios_irq_mask = simple_strtol(str+8, NULL, 0);
  403. return NULL;
  404. } else if (!strncmp(str, "lastbus=", 8)) {
  405. pcibios_last_bus = simple_strtol(str+8, NULL, 0);
  406. return NULL;
  407. }
  408. #endif
  409. else if (!strcmp(str, "rom")) {
  410. pci_probe |= PCI_ASSIGN_ROMS;
  411. return NULL;
  412. } else if (!strcmp(str, "assign-busses")) {
  413. pci_probe |= PCI_ASSIGN_ALL_BUSSES;
  414. return NULL;
  415. } else if (!strcmp(str, "use_crs")) {
  416. pci_probe |= PCI_USE__CRS;
  417. return NULL;
  418. } else if (!strcmp(str, "routeirq")) {
  419. pci_routeirq = 1;
  420. return NULL;
  421. }
  422. return str;
  423. }
  424. unsigned int pcibios_assign_all_busses(void)
  425. {
  426. return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
  427. }
  428. int pcibios_enable_device(struct pci_dev *dev, int mask)
  429. {
  430. int err;
  431. if ((err = pci_enable_resources(dev, mask)) < 0)
  432. return err;
  433. if (!dev->msi_enabled)
  434. return pcibios_enable_irq(dev);
  435. return 0;
  436. }
  437. void pcibios_disable_device (struct pci_dev *dev)
  438. {
  439. if (!dev->msi_enabled && pcibios_disable_irq)
  440. pcibios_disable_irq(dev);
  441. }
  442. struct pci_bus *__devinit pci_scan_bus_with_sysdata(int busno)
  443. {
  444. struct pci_bus *bus = NULL;
  445. struct pci_sysdata *sd;
  446. /*
  447. * Allocate per-root-bus (not per bus) arch-specific data.
  448. * TODO: leak; this memory is never freed.
  449. * It's arguable whether it's worth the trouble to care.
  450. */
  451. sd = kzalloc(sizeof(*sd), GFP_KERNEL);
  452. if (!sd) {
  453. printk(KERN_ERR "PCI: OOM, skipping PCI bus %02x\n", busno);
  454. return NULL;
  455. }
  456. sd->node = -1;
  457. bus = pci_scan_bus(busno, &pci_root_ops, sd);
  458. if (!bus)
  459. kfree(sd);
  460. return bus;
  461. }