pci.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /* pci.c: UltraSparc PCI controller support.
  2. *
  3. * Copyright (C) 1997, 1998, 1999 David S. Miller (davem@redhat.com)
  4. * Copyright (C) 1998, 1999 Eddie C. Dost (ecd@skynet.be)
  5. * Copyright (C) 1999 Jakub Jelinek (jj@ultra.linux.cz)
  6. *
  7. * OF tree based PCI bus probing taken from the PowerPC port
  8. * with minor modifications, see there for credits.
  9. */
  10. #include <linux/module.h>
  11. #include <linux/kernel.h>
  12. #include <linux/string.h>
  13. #include <linux/sched.h>
  14. #include <linux/capability.h>
  15. #include <linux/errno.h>
  16. #include <linux/pci.h>
  17. #include <linux/msi.h>
  18. #include <linux/irq.h>
  19. #include <linux/init.h>
  20. #include <linux/of.h>
  21. #include <linux/of_device.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/irq.h>
  25. #include <asm/prom.h>
  26. #include <asm/apb.h>
  27. #include "pci_impl.h"
  28. /* List of all PCI controllers found in the system. */
  29. struct pci_pbm_info *pci_pbm_root = NULL;
  30. /* Each PBM found gets a unique index. */
  31. int pci_num_pbms = 0;
  32. volatile int pci_poke_in_progress;
  33. volatile int pci_poke_cpu = -1;
  34. volatile int pci_poke_faulted;
  35. static DEFINE_SPINLOCK(pci_poke_lock);
  36. void pci_config_read8(u8 *addr, u8 *ret)
  37. {
  38. unsigned long flags;
  39. u8 byte;
  40. spin_lock_irqsave(&pci_poke_lock, flags);
  41. pci_poke_cpu = smp_processor_id();
  42. pci_poke_in_progress = 1;
  43. pci_poke_faulted = 0;
  44. __asm__ __volatile__("membar #Sync\n\t"
  45. "lduba [%1] %2, %0\n\t"
  46. "membar #Sync"
  47. : "=r" (byte)
  48. : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  49. : "memory");
  50. pci_poke_in_progress = 0;
  51. pci_poke_cpu = -1;
  52. if (!pci_poke_faulted)
  53. *ret = byte;
  54. spin_unlock_irqrestore(&pci_poke_lock, flags);
  55. }
  56. void pci_config_read16(u16 *addr, u16 *ret)
  57. {
  58. unsigned long flags;
  59. u16 word;
  60. spin_lock_irqsave(&pci_poke_lock, flags);
  61. pci_poke_cpu = smp_processor_id();
  62. pci_poke_in_progress = 1;
  63. pci_poke_faulted = 0;
  64. __asm__ __volatile__("membar #Sync\n\t"
  65. "lduha [%1] %2, %0\n\t"
  66. "membar #Sync"
  67. : "=r" (word)
  68. : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  69. : "memory");
  70. pci_poke_in_progress = 0;
  71. pci_poke_cpu = -1;
  72. if (!pci_poke_faulted)
  73. *ret = word;
  74. spin_unlock_irqrestore(&pci_poke_lock, flags);
  75. }
  76. void pci_config_read32(u32 *addr, u32 *ret)
  77. {
  78. unsigned long flags;
  79. u32 dword;
  80. spin_lock_irqsave(&pci_poke_lock, flags);
  81. pci_poke_cpu = smp_processor_id();
  82. pci_poke_in_progress = 1;
  83. pci_poke_faulted = 0;
  84. __asm__ __volatile__("membar #Sync\n\t"
  85. "lduwa [%1] %2, %0\n\t"
  86. "membar #Sync"
  87. : "=r" (dword)
  88. : "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  89. : "memory");
  90. pci_poke_in_progress = 0;
  91. pci_poke_cpu = -1;
  92. if (!pci_poke_faulted)
  93. *ret = dword;
  94. spin_unlock_irqrestore(&pci_poke_lock, flags);
  95. }
  96. void pci_config_write8(u8 *addr, u8 val)
  97. {
  98. unsigned long flags;
  99. spin_lock_irqsave(&pci_poke_lock, flags);
  100. pci_poke_cpu = smp_processor_id();
  101. pci_poke_in_progress = 1;
  102. pci_poke_faulted = 0;
  103. __asm__ __volatile__("membar #Sync\n\t"
  104. "stba %0, [%1] %2\n\t"
  105. "membar #Sync"
  106. : /* no outputs */
  107. : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  108. : "memory");
  109. pci_poke_in_progress = 0;
  110. pci_poke_cpu = -1;
  111. spin_unlock_irqrestore(&pci_poke_lock, flags);
  112. }
  113. void pci_config_write16(u16 *addr, u16 val)
  114. {
  115. unsigned long flags;
  116. spin_lock_irqsave(&pci_poke_lock, flags);
  117. pci_poke_cpu = smp_processor_id();
  118. pci_poke_in_progress = 1;
  119. pci_poke_faulted = 0;
  120. __asm__ __volatile__("membar #Sync\n\t"
  121. "stha %0, [%1] %2\n\t"
  122. "membar #Sync"
  123. : /* no outputs */
  124. : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  125. : "memory");
  126. pci_poke_in_progress = 0;
  127. pci_poke_cpu = -1;
  128. spin_unlock_irqrestore(&pci_poke_lock, flags);
  129. }
  130. void pci_config_write32(u32 *addr, u32 val)
  131. {
  132. unsigned long flags;
  133. spin_lock_irqsave(&pci_poke_lock, flags);
  134. pci_poke_cpu = smp_processor_id();
  135. pci_poke_in_progress = 1;
  136. pci_poke_faulted = 0;
  137. __asm__ __volatile__("membar #Sync\n\t"
  138. "stwa %0, [%1] %2\n\t"
  139. "membar #Sync"
  140. : /* no outputs */
  141. : "r" (val), "r" (addr), "i" (ASI_PHYS_BYPASS_EC_E_L)
  142. : "memory");
  143. pci_poke_in_progress = 0;
  144. pci_poke_cpu = -1;
  145. spin_unlock_irqrestore(&pci_poke_lock, flags);
  146. }
  147. static int ofpci_verbose;
  148. static int __init ofpci_debug(char *str)
  149. {
  150. int val = 0;
  151. get_option(&str, &val);
  152. if (val)
  153. ofpci_verbose = 1;
  154. return 1;
  155. }
  156. __setup("ofpci_debug=", ofpci_debug);
  157. static unsigned long pci_parse_of_flags(u32 addr0)
  158. {
  159. unsigned long flags = 0;
  160. if (addr0 & 0x02000000) {
  161. flags = IORESOURCE_MEM | PCI_BASE_ADDRESS_SPACE_MEMORY;
  162. flags |= (addr0 >> 22) & PCI_BASE_ADDRESS_MEM_TYPE_64;
  163. flags |= (addr0 >> 28) & PCI_BASE_ADDRESS_MEM_TYPE_1M;
  164. if (addr0 & 0x40000000)
  165. flags |= IORESOURCE_PREFETCH
  166. | PCI_BASE_ADDRESS_MEM_PREFETCH;
  167. } else if (addr0 & 0x01000000)
  168. flags = IORESOURCE_IO | PCI_BASE_ADDRESS_SPACE_IO;
  169. return flags;
  170. }
  171. /* The of_device layer has translated all of the assigned-address properties
  172. * into physical address resources, we only have to figure out the register
  173. * mapping.
  174. */
  175. static void pci_parse_of_addrs(struct of_device *op,
  176. struct device_node *node,
  177. struct pci_dev *dev)
  178. {
  179. struct resource *op_res;
  180. const u32 *addrs;
  181. int proplen;
  182. addrs = of_get_property(node, "assigned-addresses", &proplen);
  183. if (!addrs)
  184. return;
  185. if (ofpci_verbose)
  186. printk(" parse addresses (%d bytes) @ %p\n",
  187. proplen, addrs);
  188. op_res = &op->resource[0];
  189. for (; proplen >= 20; proplen -= 20, addrs += 5, op_res++) {
  190. struct resource *res;
  191. unsigned long flags;
  192. int i;
  193. flags = pci_parse_of_flags(addrs[0]);
  194. if (!flags)
  195. continue;
  196. i = addrs[0] & 0xff;
  197. if (ofpci_verbose)
  198. printk(" start: %llx, end: %llx, i: %x\n",
  199. op_res->start, op_res->end, i);
  200. if (PCI_BASE_ADDRESS_0 <= i && i <= PCI_BASE_ADDRESS_5) {
  201. res = &dev->resource[(i - PCI_BASE_ADDRESS_0) >> 2];
  202. } else if (i == dev->rom_base_reg) {
  203. res = &dev->resource[PCI_ROM_RESOURCE];
  204. flags |= IORESOURCE_READONLY | IORESOURCE_CACHEABLE;
  205. } else {
  206. printk(KERN_ERR "PCI: bad cfg reg num 0x%x\n", i);
  207. continue;
  208. }
  209. res->start = op_res->start;
  210. res->end = op_res->end;
  211. res->flags = flags;
  212. res->name = pci_name(dev);
  213. }
  214. }
  215. static struct pci_dev *of_create_pci_dev(struct pci_pbm_info *pbm,
  216. struct device_node *node,
  217. struct pci_bus *bus, int devfn)
  218. {
  219. struct dev_archdata *sd;
  220. struct of_device *op;
  221. struct pci_dev *dev;
  222. const char *type;
  223. u32 class;
  224. dev = alloc_pci_dev();
  225. if (!dev)
  226. return NULL;
  227. sd = &dev->dev.archdata;
  228. sd->iommu = pbm->iommu;
  229. sd->stc = &pbm->stc;
  230. sd->host_controller = pbm;
  231. sd->prom_node = node;
  232. sd->op = op = of_find_device_by_node(node);
  233. sd->numa_node = pbm->numa_node;
  234. sd = &op->dev.archdata;
  235. sd->iommu = pbm->iommu;
  236. sd->stc = &pbm->stc;
  237. sd->numa_node = pbm->numa_node;
  238. if (!strcmp(node->name, "ebus"))
  239. of_propagate_archdata(op);
  240. type = of_get_property(node, "device_type", NULL);
  241. if (type == NULL)
  242. type = "";
  243. if (ofpci_verbose)
  244. printk(" create device, devfn: %x, type: %s\n",
  245. devfn, type);
  246. dev->bus = bus;
  247. dev->sysdata = node;
  248. dev->dev.parent = bus->bridge;
  249. dev->dev.bus = &pci_bus_type;
  250. dev->devfn = devfn;
  251. dev->multifunction = 0; /* maybe a lie? */
  252. dev->vendor = of_getintprop_default(node, "vendor-id", 0xffff);
  253. dev->device = of_getintprop_default(node, "device-id", 0xffff);
  254. dev->subsystem_vendor =
  255. of_getintprop_default(node, "subsystem-vendor-id", 0);
  256. dev->subsystem_device =
  257. of_getintprop_default(node, "subsystem-id", 0);
  258. dev->cfg_size = pci_cfg_space_size(dev);
  259. /* We can't actually use the firmware value, we have
  260. * to read what is in the register right now. One
  261. * reason is that in the case of IDE interfaces the
  262. * firmware can sample the value before the the IDE
  263. * interface is programmed into native mode.
  264. */
  265. pci_read_config_dword(dev, PCI_CLASS_REVISION, &class);
  266. dev->class = class >> 8;
  267. dev->revision = class & 0xff;
  268. dev_set_name(&dev->dev, "%04x:%02x:%02x.%d", pci_domain_nr(bus),
  269. dev->bus->number, PCI_SLOT(devfn), PCI_FUNC(devfn));
  270. if (ofpci_verbose)
  271. printk(" class: 0x%x device name: %s\n",
  272. dev->class, pci_name(dev));
  273. /* I have seen IDE devices which will not respond to
  274. * the bmdma simplex check reads if bus mastering is
  275. * disabled.
  276. */
  277. if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE)
  278. pci_set_master(dev);
  279. dev->current_state = 4; /* unknown power state */
  280. dev->error_state = pci_channel_io_normal;
  281. if (!strcmp(node->name, "pci")) {
  282. /* a PCI-PCI bridge */
  283. dev->hdr_type = PCI_HEADER_TYPE_BRIDGE;
  284. dev->rom_base_reg = PCI_ROM_ADDRESS1;
  285. } else if (!strcmp(type, "cardbus")) {
  286. dev->hdr_type = PCI_HEADER_TYPE_CARDBUS;
  287. } else {
  288. dev->hdr_type = PCI_HEADER_TYPE_NORMAL;
  289. dev->rom_base_reg = PCI_ROM_ADDRESS;
  290. dev->irq = sd->op->irqs[0];
  291. if (dev->irq == 0xffffffff)
  292. dev->irq = PCI_IRQ_NONE;
  293. }
  294. pci_parse_of_addrs(sd->op, node, dev);
  295. if (ofpci_verbose)
  296. printk(" adding to system ...\n");
  297. pci_device_add(dev, bus);
  298. return dev;
  299. }
  300. static void __devinit apb_calc_first_last(u8 map, u32 *first_p, u32 *last_p)
  301. {
  302. u32 idx, first, last;
  303. first = 8;
  304. last = 0;
  305. for (idx = 0; idx < 8; idx++) {
  306. if ((map & (1 << idx)) != 0) {
  307. if (first > idx)
  308. first = idx;
  309. if (last < idx)
  310. last = idx;
  311. }
  312. }
  313. *first_p = first;
  314. *last_p = last;
  315. }
  316. static void pci_resource_adjust(struct resource *res,
  317. struct resource *root)
  318. {
  319. res->start += root->start;
  320. res->end += root->start;
  321. }
  322. /* For PCI bus devices which lack a 'ranges' property we interrogate
  323. * the config space values to set the resources, just like the generic
  324. * Linux PCI probing code does.
  325. */
  326. static void __devinit pci_cfg_fake_ranges(struct pci_dev *dev,
  327. struct pci_bus *bus,
  328. struct pci_pbm_info *pbm)
  329. {
  330. struct resource *res;
  331. u8 io_base_lo, io_limit_lo;
  332. u16 mem_base_lo, mem_limit_lo;
  333. unsigned long base, limit;
  334. pci_read_config_byte(dev, PCI_IO_BASE, &io_base_lo);
  335. pci_read_config_byte(dev, PCI_IO_LIMIT, &io_limit_lo);
  336. base = (io_base_lo & PCI_IO_RANGE_MASK) << 8;
  337. limit = (io_limit_lo & PCI_IO_RANGE_MASK) << 8;
  338. if ((io_base_lo & PCI_IO_RANGE_TYPE_MASK) == PCI_IO_RANGE_TYPE_32) {
  339. u16 io_base_hi, io_limit_hi;
  340. pci_read_config_word(dev, PCI_IO_BASE_UPPER16, &io_base_hi);
  341. pci_read_config_word(dev, PCI_IO_LIMIT_UPPER16, &io_limit_hi);
  342. base |= (io_base_hi << 16);
  343. limit |= (io_limit_hi << 16);
  344. }
  345. res = bus->resource[0];
  346. if (base <= limit) {
  347. res->flags = (io_base_lo & PCI_IO_RANGE_TYPE_MASK) | IORESOURCE_IO;
  348. if (!res->start)
  349. res->start = base;
  350. if (!res->end)
  351. res->end = limit + 0xfff;
  352. pci_resource_adjust(res, &pbm->io_space);
  353. }
  354. pci_read_config_word(dev, PCI_MEMORY_BASE, &mem_base_lo);
  355. pci_read_config_word(dev, PCI_MEMORY_LIMIT, &mem_limit_lo);
  356. base = (mem_base_lo & PCI_MEMORY_RANGE_MASK) << 16;
  357. limit = (mem_limit_lo & PCI_MEMORY_RANGE_MASK) << 16;
  358. res = bus->resource[1];
  359. if (base <= limit) {
  360. res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
  361. IORESOURCE_MEM);
  362. res->start = base;
  363. res->end = limit + 0xfffff;
  364. pci_resource_adjust(res, &pbm->mem_space);
  365. }
  366. pci_read_config_word(dev, PCI_PREF_MEMORY_BASE, &mem_base_lo);
  367. pci_read_config_word(dev, PCI_PREF_MEMORY_LIMIT, &mem_limit_lo);
  368. base = (mem_base_lo & PCI_PREF_RANGE_MASK) << 16;
  369. limit = (mem_limit_lo & PCI_PREF_RANGE_MASK) << 16;
  370. if ((mem_base_lo & PCI_PREF_RANGE_TYPE_MASK) == PCI_PREF_RANGE_TYPE_64) {
  371. u32 mem_base_hi, mem_limit_hi;
  372. pci_read_config_dword(dev, PCI_PREF_BASE_UPPER32, &mem_base_hi);
  373. pci_read_config_dword(dev, PCI_PREF_LIMIT_UPPER32, &mem_limit_hi);
  374. /*
  375. * Some bridges set the base > limit by default, and some
  376. * (broken) BIOSes do not initialize them. If we find
  377. * this, just assume they are not being used.
  378. */
  379. if (mem_base_hi <= mem_limit_hi) {
  380. base |= ((long) mem_base_hi) << 32;
  381. limit |= ((long) mem_limit_hi) << 32;
  382. }
  383. }
  384. res = bus->resource[2];
  385. if (base <= limit) {
  386. res->flags = ((mem_base_lo & PCI_MEMORY_RANGE_TYPE_MASK) |
  387. IORESOURCE_MEM | IORESOURCE_PREFETCH);
  388. res->start = base;
  389. res->end = limit + 0xfffff;
  390. pci_resource_adjust(res, &pbm->mem_space);
  391. }
  392. }
  393. /* Cook up fake bus resources for SUNW,simba PCI bridges which lack
  394. * a proper 'ranges' property.
  395. */
  396. static void __devinit apb_fake_ranges(struct pci_dev *dev,
  397. struct pci_bus *bus,
  398. struct pci_pbm_info *pbm)
  399. {
  400. struct resource *res;
  401. u32 first, last;
  402. u8 map;
  403. pci_read_config_byte(dev, APB_IO_ADDRESS_MAP, &map);
  404. apb_calc_first_last(map, &first, &last);
  405. res = bus->resource[0];
  406. res->start = (first << 21);
  407. res->end = (last << 21) + ((1 << 21) - 1);
  408. res->flags = IORESOURCE_IO;
  409. pci_resource_adjust(res, &pbm->io_space);
  410. pci_read_config_byte(dev, APB_MEM_ADDRESS_MAP, &map);
  411. apb_calc_first_last(map, &first, &last);
  412. res = bus->resource[1];
  413. res->start = (first << 21);
  414. res->end = (last << 21) + ((1 << 21) - 1);
  415. res->flags = IORESOURCE_MEM;
  416. pci_resource_adjust(res, &pbm->mem_space);
  417. }
  418. static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
  419. struct device_node *node,
  420. struct pci_bus *bus);
  421. #define GET_64BIT(prop, i) ((((u64) (prop)[(i)]) << 32) | (prop)[(i)+1])
  422. static void __devinit of_scan_pci_bridge(struct pci_pbm_info *pbm,
  423. struct device_node *node,
  424. struct pci_dev *dev)
  425. {
  426. struct pci_bus *bus;
  427. const u32 *busrange, *ranges;
  428. int len, i, simba;
  429. struct resource *res;
  430. unsigned int flags;
  431. u64 size;
  432. if (ofpci_verbose)
  433. printk("of_scan_pci_bridge(%s)\n", node->full_name);
  434. /* parse bus-range property */
  435. busrange = of_get_property(node, "bus-range", &len);
  436. if (busrange == NULL || len != 8) {
  437. printk(KERN_DEBUG "Can't get bus-range for PCI-PCI bridge %s\n",
  438. node->full_name);
  439. return;
  440. }
  441. ranges = of_get_property(node, "ranges", &len);
  442. simba = 0;
  443. if (ranges == NULL) {
  444. const char *model = of_get_property(node, "model", NULL);
  445. if (model && !strcmp(model, "SUNW,simba"))
  446. simba = 1;
  447. }
  448. bus = pci_add_new_bus(dev->bus, dev, busrange[0]);
  449. if (!bus) {
  450. printk(KERN_ERR "Failed to create pci bus for %s\n",
  451. node->full_name);
  452. return;
  453. }
  454. bus->primary = dev->bus->number;
  455. bus->subordinate = busrange[1];
  456. bus->bridge_ctl = 0;
  457. /* parse ranges property, or cook one up by hand for Simba */
  458. /* PCI #address-cells == 3 and #size-cells == 2 always */
  459. res = &dev->resource[PCI_BRIDGE_RESOURCES];
  460. for (i = 0; i < PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES; ++i) {
  461. res->flags = 0;
  462. bus->resource[i] = res;
  463. ++res;
  464. }
  465. if (simba) {
  466. apb_fake_ranges(dev, bus, pbm);
  467. goto after_ranges;
  468. } else if (ranges == NULL) {
  469. pci_cfg_fake_ranges(dev, bus, pbm);
  470. goto after_ranges;
  471. }
  472. i = 1;
  473. for (; len >= 32; len -= 32, ranges += 8) {
  474. struct resource *root;
  475. flags = pci_parse_of_flags(ranges[0]);
  476. size = GET_64BIT(ranges, 6);
  477. if (flags == 0 || size == 0)
  478. continue;
  479. if (flags & IORESOURCE_IO) {
  480. res = bus->resource[0];
  481. if (res->flags) {
  482. printk(KERN_ERR "PCI: ignoring extra I/O range"
  483. " for bridge %s\n", node->full_name);
  484. continue;
  485. }
  486. root = &pbm->io_space;
  487. } else {
  488. if (i >= PCI_NUM_RESOURCES - PCI_BRIDGE_RESOURCES) {
  489. printk(KERN_ERR "PCI: too many memory ranges"
  490. " for bridge %s\n", node->full_name);
  491. continue;
  492. }
  493. res = bus->resource[i];
  494. ++i;
  495. root = &pbm->mem_space;
  496. }
  497. res->start = GET_64BIT(ranges, 1);
  498. res->end = res->start + size - 1;
  499. res->flags = flags;
  500. /* Another way to implement this would be to add an of_device
  501. * layer routine that can calculate a resource for a given
  502. * range property value in a PCI device.
  503. */
  504. pci_resource_adjust(res, root);
  505. }
  506. after_ranges:
  507. sprintf(bus->name, "PCI Bus %04x:%02x", pci_domain_nr(bus),
  508. bus->number);
  509. if (ofpci_verbose)
  510. printk(" bus name: %s\n", bus->name);
  511. pci_of_scan_bus(pbm, node, bus);
  512. }
  513. static void __devinit pci_of_scan_bus(struct pci_pbm_info *pbm,
  514. struct device_node *node,
  515. struct pci_bus *bus)
  516. {
  517. struct device_node *child;
  518. const u32 *reg;
  519. int reglen, devfn, prev_devfn;
  520. struct pci_dev *dev;
  521. if (ofpci_verbose)
  522. printk("PCI: scan_bus[%s] bus no %d\n",
  523. node->full_name, bus->number);
  524. child = NULL;
  525. prev_devfn = -1;
  526. while ((child = of_get_next_child(node, child)) != NULL) {
  527. if (ofpci_verbose)
  528. printk(" * %s\n", child->full_name);
  529. reg = of_get_property(child, "reg", &reglen);
  530. if (reg == NULL || reglen < 20)
  531. continue;
  532. devfn = (reg[0] >> 8) & 0xff;
  533. /* This is a workaround for some device trees
  534. * which list PCI devices twice. On the V100
  535. * for example, device number 3 is listed twice.
  536. * Once as "pm" and once again as "lomp".
  537. */
  538. if (devfn == prev_devfn)
  539. continue;
  540. prev_devfn = devfn;
  541. /* create a new pci_dev for this device */
  542. dev = of_create_pci_dev(pbm, child, bus, devfn);
  543. if (!dev)
  544. continue;
  545. if (ofpci_verbose)
  546. printk("PCI: dev header type: %x\n",
  547. dev->hdr_type);
  548. if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE ||
  549. dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)
  550. of_scan_pci_bridge(pbm, child, dev);
  551. }
  552. }
  553. static ssize_t
  554. show_pciobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
  555. {
  556. struct pci_dev *pdev;
  557. struct device_node *dp;
  558. pdev = to_pci_dev(dev);
  559. dp = pdev->dev.archdata.prom_node;
  560. return snprintf (buf, PAGE_SIZE, "%s\n", dp->full_name);
  561. }
  562. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_pciobppath_attr, NULL);
  563. static void __devinit pci_bus_register_of_sysfs(struct pci_bus *bus)
  564. {
  565. struct pci_dev *dev;
  566. struct pci_bus *child_bus;
  567. int err;
  568. list_for_each_entry(dev, &bus->devices, bus_list) {
  569. /* we don't really care if we can create this file or
  570. * not, but we need to assign the result of the call
  571. * or the world will fall under alien invasion and
  572. * everybody will be frozen on a spaceship ready to be
  573. * eaten on alpha centauri by some green and jelly
  574. * humanoid.
  575. */
  576. err = sysfs_create_file(&dev->dev.kobj, &dev_attr_obppath.attr);
  577. }
  578. list_for_each_entry(child_bus, &bus->children, node)
  579. pci_bus_register_of_sysfs(child_bus);
  580. }
  581. struct pci_bus * __devinit pci_scan_one_pbm(struct pci_pbm_info *pbm,
  582. struct device *parent)
  583. {
  584. struct device_node *node = pbm->op->node;
  585. struct pci_bus *bus;
  586. printk("PCI: Scanning PBM %s\n", node->full_name);
  587. bus = pci_create_bus(parent, pbm->pci_first_busno, pbm->pci_ops, pbm);
  588. if (!bus) {
  589. printk(KERN_ERR "Failed to create bus for %s\n",
  590. node->full_name);
  591. return NULL;
  592. }
  593. bus->secondary = pbm->pci_first_busno;
  594. bus->subordinate = pbm->pci_last_busno;
  595. bus->resource[0] = &pbm->io_space;
  596. bus->resource[1] = &pbm->mem_space;
  597. pci_of_scan_bus(pbm, node, bus);
  598. pci_bus_add_devices(bus);
  599. pci_bus_register_of_sysfs(bus);
  600. return bus;
  601. }
  602. void __devinit pcibios_fixup_bus(struct pci_bus *pbus)
  603. {
  604. struct pci_pbm_info *pbm = pbus->sysdata;
  605. /* Generic PCI bus probing sets these to point at
  606. * &io{port,mem}_resouce which is wrong for us.
  607. */
  608. pbus->resource[0] = &pbm->io_space;
  609. pbus->resource[1] = &pbm->mem_space;
  610. }
  611. struct resource *pcibios_select_root(struct pci_dev *pdev, struct resource *r)
  612. {
  613. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  614. struct resource *root = NULL;
  615. if (r->flags & IORESOURCE_IO)
  616. root = &pbm->io_space;
  617. if (r->flags & IORESOURCE_MEM)
  618. root = &pbm->mem_space;
  619. return root;
  620. }
  621. void pcibios_update_irq(struct pci_dev *pdev, int irq)
  622. {
  623. }
  624. void pcibios_align_resource(void *data, struct resource *res,
  625. resource_size_t size, resource_size_t align)
  626. {
  627. }
  628. int pcibios_enable_device(struct pci_dev *dev, int mask)
  629. {
  630. u16 cmd, oldcmd;
  631. int i;
  632. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  633. oldcmd = cmd;
  634. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  635. struct resource *res = &dev->resource[i];
  636. /* Only set up the requested stuff */
  637. if (!(mask & (1<<i)))
  638. continue;
  639. if (res->flags & IORESOURCE_IO)
  640. cmd |= PCI_COMMAND_IO;
  641. if (res->flags & IORESOURCE_MEM)
  642. cmd |= PCI_COMMAND_MEMORY;
  643. }
  644. if (cmd != oldcmd) {
  645. printk(KERN_DEBUG "PCI: Enabling device: (%s), cmd %x\n",
  646. pci_name(dev), cmd);
  647. /* Enable the appropriate bits in the PCI command register. */
  648. pci_write_config_word(dev, PCI_COMMAND, cmd);
  649. }
  650. return 0;
  651. }
  652. void pcibios_resource_to_bus(struct pci_dev *pdev, struct pci_bus_region *region,
  653. struct resource *res)
  654. {
  655. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  656. struct resource zero_res, *root;
  657. zero_res.start = 0;
  658. zero_res.end = 0;
  659. zero_res.flags = res->flags;
  660. if (res->flags & IORESOURCE_IO)
  661. root = &pbm->io_space;
  662. else
  663. root = &pbm->mem_space;
  664. pci_resource_adjust(&zero_res, root);
  665. region->start = res->start - zero_res.start;
  666. region->end = res->end - zero_res.start;
  667. }
  668. EXPORT_SYMBOL(pcibios_resource_to_bus);
  669. void pcibios_bus_to_resource(struct pci_dev *pdev, struct resource *res,
  670. struct pci_bus_region *region)
  671. {
  672. struct pci_pbm_info *pbm = pdev->bus->sysdata;
  673. struct resource *root;
  674. res->start = region->start;
  675. res->end = region->end;
  676. if (res->flags & IORESOURCE_IO)
  677. root = &pbm->io_space;
  678. else
  679. root = &pbm->mem_space;
  680. pci_resource_adjust(res, root);
  681. }
  682. EXPORT_SYMBOL(pcibios_bus_to_resource);
  683. char * __devinit pcibios_setup(char *str)
  684. {
  685. return str;
  686. }
  687. /* Platform support for /proc/bus/pci/X/Y mmap()s. */
  688. /* If the user uses a host-bridge as the PCI device, he may use
  689. * this to perform a raw mmap() of the I/O or MEM space behind
  690. * that controller.
  691. *
  692. * This can be useful for execution of x86 PCI bios initialization code
  693. * on a PCI card, like the xfree86 int10 stuff does.
  694. */
  695. static int __pci_mmap_make_offset_bus(struct pci_dev *pdev, struct vm_area_struct *vma,
  696. enum pci_mmap_state mmap_state)
  697. {
  698. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  699. unsigned long space_size, user_offset, user_size;
  700. if (mmap_state == pci_mmap_io) {
  701. space_size = (pbm->io_space.end -
  702. pbm->io_space.start) + 1;
  703. } else {
  704. space_size = (pbm->mem_space.end -
  705. pbm->mem_space.start) + 1;
  706. }
  707. /* Make sure the request is in range. */
  708. user_offset = vma->vm_pgoff << PAGE_SHIFT;
  709. user_size = vma->vm_end - vma->vm_start;
  710. if (user_offset >= space_size ||
  711. (user_offset + user_size) > space_size)
  712. return -EINVAL;
  713. if (mmap_state == pci_mmap_io) {
  714. vma->vm_pgoff = (pbm->io_space.start +
  715. user_offset) >> PAGE_SHIFT;
  716. } else {
  717. vma->vm_pgoff = (pbm->mem_space.start +
  718. user_offset) >> PAGE_SHIFT;
  719. }
  720. return 0;
  721. }
  722. /* Adjust vm_pgoff of VMA such that it is the physical page offset
  723. * corresponding to the 32-bit pci bus offset for DEV requested by the user.
  724. *
  725. * Basically, the user finds the base address for his device which he wishes
  726. * to mmap. They read the 32-bit value from the config space base register,
  727. * add whatever PAGE_SIZE multiple offset they wish, and feed this into the
  728. * offset parameter of mmap on /proc/bus/pci/XXX for that device.
  729. *
  730. * Returns negative error code on failure, zero on success.
  731. */
  732. static int __pci_mmap_make_offset(struct pci_dev *pdev,
  733. struct vm_area_struct *vma,
  734. enum pci_mmap_state mmap_state)
  735. {
  736. unsigned long user_paddr, user_size;
  737. int i, err;
  738. /* First compute the physical address in vma->vm_pgoff,
  739. * making sure the user offset is within range in the
  740. * appropriate PCI space.
  741. */
  742. err = __pci_mmap_make_offset_bus(pdev, vma, mmap_state);
  743. if (err)
  744. return err;
  745. /* If this is a mapping on a host bridge, any address
  746. * is OK.
  747. */
  748. if ((pdev->class >> 8) == PCI_CLASS_BRIDGE_HOST)
  749. return err;
  750. /* Otherwise make sure it's in the range for one of the
  751. * device's resources.
  752. */
  753. user_paddr = vma->vm_pgoff << PAGE_SHIFT;
  754. user_size = vma->vm_end - vma->vm_start;
  755. for (i = 0; i <= PCI_ROM_RESOURCE; i++) {
  756. struct resource *rp = &pdev->resource[i];
  757. resource_size_t aligned_end;
  758. /* Active? */
  759. if (!rp->flags)
  760. continue;
  761. /* Same type? */
  762. if (i == PCI_ROM_RESOURCE) {
  763. if (mmap_state != pci_mmap_mem)
  764. continue;
  765. } else {
  766. if ((mmap_state == pci_mmap_io &&
  767. (rp->flags & IORESOURCE_IO) == 0) ||
  768. (mmap_state == pci_mmap_mem &&
  769. (rp->flags & IORESOURCE_MEM) == 0))
  770. continue;
  771. }
  772. /* Align the resource end to the next page address.
  773. * PAGE_SIZE intentionally added instead of (PAGE_SIZE - 1),
  774. * because actually we need the address of the next byte
  775. * after rp->end.
  776. */
  777. aligned_end = (rp->end + PAGE_SIZE) & PAGE_MASK;
  778. if ((rp->start <= user_paddr) &&
  779. (user_paddr + user_size) <= aligned_end)
  780. break;
  781. }
  782. if (i > PCI_ROM_RESOURCE)
  783. return -EINVAL;
  784. return 0;
  785. }
  786. /* Set vm_flags of VMA, as appropriate for this architecture, for a pci device
  787. * mapping.
  788. */
  789. static void __pci_mmap_set_flags(struct pci_dev *dev, struct vm_area_struct *vma,
  790. enum pci_mmap_state mmap_state)
  791. {
  792. vma->vm_flags |= (VM_IO | VM_RESERVED);
  793. }
  794. /* Set vm_page_prot of VMA, as appropriate for this architecture, for a pci
  795. * device mapping.
  796. */
  797. static void __pci_mmap_set_pgprot(struct pci_dev *dev, struct vm_area_struct *vma,
  798. enum pci_mmap_state mmap_state)
  799. {
  800. /* Our io_remap_pfn_range takes care of this, do nothing. */
  801. }
  802. /* Perform the actual remap of the pages for a PCI device mapping, as appropriate
  803. * for this architecture. The region in the process to map is described by vm_start
  804. * and vm_end members of VMA, the base physical address is found in vm_pgoff.
  805. * The pci device structure is provided so that architectures may make mapping
  806. * decisions on a per-device or per-bus basis.
  807. *
  808. * Returns a negative error code on failure, zero on success.
  809. */
  810. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  811. enum pci_mmap_state mmap_state,
  812. int write_combine)
  813. {
  814. int ret;
  815. ret = __pci_mmap_make_offset(dev, vma, mmap_state);
  816. if (ret < 0)
  817. return ret;
  818. __pci_mmap_set_flags(dev, vma, mmap_state);
  819. __pci_mmap_set_pgprot(dev, vma, mmap_state);
  820. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  821. ret = io_remap_pfn_range(vma, vma->vm_start,
  822. vma->vm_pgoff,
  823. vma->vm_end - vma->vm_start,
  824. vma->vm_page_prot);
  825. if (ret)
  826. return ret;
  827. return 0;
  828. }
  829. #ifdef CONFIG_NUMA
  830. int pcibus_to_node(struct pci_bus *pbus)
  831. {
  832. struct pci_pbm_info *pbm = pbus->sysdata;
  833. return pbm->numa_node;
  834. }
  835. EXPORT_SYMBOL(pcibus_to_node);
  836. #endif
  837. /* Return the domain number for this pci bus */
  838. int pci_domain_nr(struct pci_bus *pbus)
  839. {
  840. struct pci_pbm_info *pbm = pbus->sysdata;
  841. int ret;
  842. if (!pbm) {
  843. ret = -ENXIO;
  844. } else {
  845. ret = pbm->index;
  846. }
  847. return ret;
  848. }
  849. EXPORT_SYMBOL(pci_domain_nr);
  850. #ifdef CONFIG_PCI_MSI
  851. int arch_setup_msi_irq(struct pci_dev *pdev, struct msi_desc *desc)
  852. {
  853. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  854. unsigned int virt_irq;
  855. if (!pbm->setup_msi_irq)
  856. return -EINVAL;
  857. return pbm->setup_msi_irq(&virt_irq, pdev, desc);
  858. }
  859. void arch_teardown_msi_irq(unsigned int virt_irq)
  860. {
  861. struct msi_desc *entry = get_irq_msi(virt_irq);
  862. struct pci_dev *pdev = entry->dev;
  863. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  864. if (pbm->teardown_msi_irq)
  865. pbm->teardown_msi_irq(virt_irq, pdev);
  866. }
  867. #endif /* !(CONFIG_PCI_MSI) */
  868. struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
  869. {
  870. return pdev->dev.archdata.prom_node;
  871. }
  872. EXPORT_SYMBOL(pci_device_to_OF_node);
  873. static void ali_sound_dma_hack(struct pci_dev *pdev, int set_bit)
  874. {
  875. struct pci_dev *ali_isa_bridge;
  876. u8 val;
  877. /* ALI sound chips generate 31-bits of DMA, a special register
  878. * determines what bit 31 is emitted as.
  879. */
  880. ali_isa_bridge = pci_get_device(PCI_VENDOR_ID_AL,
  881. PCI_DEVICE_ID_AL_M1533,
  882. NULL);
  883. pci_read_config_byte(ali_isa_bridge, 0x7e, &val);
  884. if (set_bit)
  885. val |= 0x01;
  886. else
  887. val &= ~0x01;
  888. pci_write_config_byte(ali_isa_bridge, 0x7e, val);
  889. pci_dev_put(ali_isa_bridge);
  890. }
  891. int pci_dma_supported(struct pci_dev *pdev, u64 device_mask)
  892. {
  893. u64 dma_addr_mask;
  894. if (pdev == NULL) {
  895. dma_addr_mask = 0xffffffff;
  896. } else {
  897. struct iommu *iommu = pdev->dev.archdata.iommu;
  898. dma_addr_mask = iommu->dma_addr_mask;
  899. if (pdev->vendor == PCI_VENDOR_ID_AL &&
  900. pdev->device == PCI_DEVICE_ID_AL_M5451 &&
  901. device_mask == 0x7fffffff) {
  902. ali_sound_dma_hack(pdev,
  903. (dma_addr_mask & 0x80000000) != 0);
  904. return 1;
  905. }
  906. }
  907. if (device_mask >= (1UL << 32UL))
  908. return 0;
  909. return (device_mask & dma_addr_mask) == dma_addr_mask;
  910. }
  911. EXPORT_SYMBOL(pci_dma_supported);
  912. void pci_resource_to_user(const struct pci_dev *pdev, int bar,
  913. const struct resource *rp, resource_size_t *start,
  914. resource_size_t *end)
  915. {
  916. struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
  917. unsigned long offset;
  918. if (rp->flags & IORESOURCE_IO)
  919. offset = pbm->io_space.start;
  920. else
  921. offset = pbm->mem_space.start;
  922. *start = rp->start - offset;
  923. *end = rp->end - offset;
  924. }