pci.c 28 KB

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