pci.c 31 KB

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