pci.c 25 KB

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