pci.c 31 KB

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