pci.c 30 KB

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