pci.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. /*
  2. * Copyright IBM Corp. 2012
  3. *
  4. * Author(s):
  5. * Jan Glauber <jang@linux.vnet.ibm.com>
  6. *
  7. * The System z PCI code is a rewrite from a prototype by
  8. * the following people (Kudoz!):
  9. * Alexander Schmidt
  10. * Christoph Raisch
  11. * Hannes Hering
  12. * Hoang-Nam Nguyen
  13. * Jan-Bernd Themann
  14. * Stefan Roscher
  15. * Thomas Klein
  16. */
  17. #define COMPONENT "zPCI"
  18. #define pr_fmt(fmt) COMPONENT ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/err.h>
  22. #include <linux/export.h>
  23. #include <linux/delay.h>
  24. #include <linux/irq.h>
  25. #include <linux/kernel_stat.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/pci.h>
  28. #include <linux/msi.h>
  29. #include <asm/isc.h>
  30. #include <asm/airq.h>
  31. #include <asm/facility.h>
  32. #include <asm/pci_insn.h>
  33. #include <asm/pci_clp.h>
  34. #include <asm/pci_dma.h>
  35. #define DEBUG /* enable pr_debug */
  36. #define SIC_IRQ_MODE_ALL 0
  37. #define SIC_IRQ_MODE_SINGLE 1
  38. #define ZPCI_NR_DMA_SPACES 1
  39. #define ZPCI_MSI_VEC_BITS 6
  40. #define ZPCI_NR_DEVICES CONFIG_PCI_NR_FUNCTIONS
  41. /* list of all detected zpci devices */
  42. LIST_HEAD(zpci_list);
  43. EXPORT_SYMBOL_GPL(zpci_list);
  44. DEFINE_MUTEX(zpci_list_lock);
  45. EXPORT_SYMBOL_GPL(zpci_list_lock);
  46. static struct pci_hp_callback_ops *hotplug_ops;
  47. static DECLARE_BITMAP(zpci_domain, ZPCI_NR_DEVICES);
  48. static DEFINE_SPINLOCK(zpci_domain_lock);
  49. struct callback {
  50. irq_handler_t handler;
  51. void *data;
  52. };
  53. struct zdev_irq_map {
  54. unsigned long aibv; /* AI bit vector */
  55. int msi_vecs; /* consecutive MSI-vectors used */
  56. int __unused;
  57. struct callback cb[ZPCI_NR_MSI_VECS]; /* callback handler array */
  58. spinlock_t lock; /* protect callbacks against de-reg */
  59. };
  60. struct intr_bucket {
  61. /* amap of adapters, one bit per dev, corresponds to one irq nr */
  62. unsigned long *alloc;
  63. /* AI summary bit, global page for all devices */
  64. unsigned long *aisb;
  65. /* pointer to aibv and callback data in zdev */
  66. struct zdev_irq_map *imap[ZPCI_NR_DEVICES];
  67. /* protects the whole bucket struct */
  68. spinlock_t lock;
  69. };
  70. static struct intr_bucket *bucket;
  71. /* Adapter local summary indicator */
  72. static u8 *zpci_irq_si;
  73. static atomic_t irq_retries = ATOMIC_INIT(0);
  74. /* I/O Map */
  75. static DEFINE_SPINLOCK(zpci_iomap_lock);
  76. static DECLARE_BITMAP(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
  77. struct zpci_iomap_entry *zpci_iomap_start;
  78. EXPORT_SYMBOL_GPL(zpci_iomap_start);
  79. /* highest irq summary bit */
  80. static int __read_mostly aisb_max;
  81. static struct kmem_cache *zdev_irq_cache;
  82. static struct kmem_cache *zdev_fmb_cache;
  83. static inline int irq_to_msi_nr(unsigned int irq)
  84. {
  85. return irq & ZPCI_MSI_MASK;
  86. }
  87. static inline int irq_to_dev_nr(unsigned int irq)
  88. {
  89. return irq >> ZPCI_MSI_VEC_BITS;
  90. }
  91. static inline struct zdev_irq_map *get_imap(unsigned int irq)
  92. {
  93. return bucket->imap[irq_to_dev_nr(irq)];
  94. }
  95. struct zpci_dev *get_zdev(struct pci_dev *pdev)
  96. {
  97. return (struct zpci_dev *) pdev->sysdata;
  98. }
  99. struct zpci_dev *get_zdev_by_fid(u32 fid)
  100. {
  101. struct zpci_dev *tmp, *zdev = NULL;
  102. mutex_lock(&zpci_list_lock);
  103. list_for_each_entry(tmp, &zpci_list, entry) {
  104. if (tmp->fid == fid) {
  105. zdev = tmp;
  106. break;
  107. }
  108. }
  109. mutex_unlock(&zpci_list_lock);
  110. return zdev;
  111. }
  112. bool zpci_fid_present(u32 fid)
  113. {
  114. return (get_zdev_by_fid(fid) != NULL) ? true : false;
  115. }
  116. static struct zpci_dev *get_zdev_by_bus(struct pci_bus *bus)
  117. {
  118. return (bus && bus->sysdata) ? (struct zpci_dev *) bus->sysdata : NULL;
  119. }
  120. int pci_domain_nr(struct pci_bus *bus)
  121. {
  122. return ((struct zpci_dev *) bus->sysdata)->domain;
  123. }
  124. EXPORT_SYMBOL_GPL(pci_domain_nr);
  125. int pci_proc_domain(struct pci_bus *bus)
  126. {
  127. return pci_domain_nr(bus);
  128. }
  129. EXPORT_SYMBOL_GPL(pci_proc_domain);
  130. /* Modify PCI: Register adapter interruptions */
  131. static int zpci_register_airq(struct zpci_dev *zdev, unsigned int aisb,
  132. u64 aibv)
  133. {
  134. u64 req = ZPCI_CREATE_REQ(zdev->fh, 0, ZPCI_MOD_FC_REG_INT);
  135. struct zpci_fib *fib;
  136. int rc;
  137. fib = (void *) get_zeroed_page(GFP_KERNEL);
  138. if (!fib)
  139. return -ENOMEM;
  140. fib->isc = PCI_ISC;
  141. fib->noi = zdev->irq_map->msi_vecs;
  142. fib->sum = 1; /* enable summary notifications */
  143. fib->aibv = aibv;
  144. fib->aibvo = 0; /* every function has its own page */
  145. fib->aisb = (u64) bucket->aisb + aisb / 8;
  146. fib->aisbo = aisb & ZPCI_MSI_MASK;
  147. rc = s390pci_mod_fc(req, fib);
  148. pr_debug("%s mpcifc returned noi: %d\n", __func__, fib->noi);
  149. free_page((unsigned long) fib);
  150. return rc;
  151. }
  152. struct mod_pci_args {
  153. u64 base;
  154. u64 limit;
  155. u64 iota;
  156. u64 fmb_addr;
  157. };
  158. static int mod_pci(struct zpci_dev *zdev, int fn, u8 dmaas, struct mod_pci_args *args)
  159. {
  160. u64 req = ZPCI_CREATE_REQ(zdev->fh, dmaas, fn);
  161. struct zpci_fib *fib;
  162. int rc;
  163. /* The FIB must be available even if it's not used */
  164. fib = (void *) get_zeroed_page(GFP_KERNEL);
  165. if (!fib)
  166. return -ENOMEM;
  167. fib->pba = args->base;
  168. fib->pal = args->limit;
  169. fib->iota = args->iota;
  170. fib->fmb_addr = args->fmb_addr;
  171. rc = s390pci_mod_fc(req, fib);
  172. free_page((unsigned long) fib);
  173. return rc;
  174. }
  175. /* Modify PCI: Register I/O address translation parameters */
  176. int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,
  177. u64 base, u64 limit, u64 iota)
  178. {
  179. struct mod_pci_args args = { base, limit, iota, 0 };
  180. WARN_ON_ONCE(iota & 0x3fff);
  181. args.iota |= ZPCI_IOTA_RTTO_FLAG;
  182. return mod_pci(zdev, ZPCI_MOD_FC_REG_IOAT, dmaas, &args);
  183. }
  184. /* Modify PCI: Unregister I/O address translation parameters */
  185. int zpci_unregister_ioat(struct zpci_dev *zdev, u8 dmaas)
  186. {
  187. struct mod_pci_args args = { 0, 0, 0, 0 };
  188. return mod_pci(zdev, ZPCI_MOD_FC_DEREG_IOAT, dmaas, &args);
  189. }
  190. /* Modify PCI: Unregister adapter interruptions */
  191. static int zpci_unregister_airq(struct zpci_dev *zdev)
  192. {
  193. struct mod_pci_args args = { 0, 0, 0, 0 };
  194. return mod_pci(zdev, ZPCI_MOD_FC_DEREG_INT, 0, &args);
  195. }
  196. /* Modify PCI: Set PCI function measurement parameters */
  197. int zpci_fmb_enable_device(struct zpci_dev *zdev)
  198. {
  199. struct mod_pci_args args = { 0, 0, 0, 0 };
  200. if (zdev->fmb)
  201. return -EINVAL;
  202. zdev->fmb = kmem_cache_zalloc(zdev_fmb_cache, GFP_KERNEL);
  203. if (!zdev->fmb)
  204. return -ENOMEM;
  205. WARN_ON((u64) zdev->fmb & 0xf);
  206. args.fmb_addr = virt_to_phys(zdev->fmb);
  207. return mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
  208. }
  209. /* Modify PCI: Disable PCI function measurement */
  210. int zpci_fmb_disable_device(struct zpci_dev *zdev)
  211. {
  212. struct mod_pci_args args = { 0, 0, 0, 0 };
  213. int rc;
  214. if (!zdev->fmb)
  215. return -EINVAL;
  216. /* Function measurement is disabled if fmb address is zero */
  217. rc = mod_pci(zdev, ZPCI_MOD_FC_SET_MEASURE, 0, &args);
  218. kmem_cache_free(zdev_fmb_cache, zdev->fmb);
  219. zdev->fmb = NULL;
  220. return rc;
  221. }
  222. #define ZPCI_PCIAS_CFGSPC 15
  223. static int zpci_cfg_load(struct zpci_dev *zdev, int offset, u32 *val, u8 len)
  224. {
  225. u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
  226. u64 data;
  227. int rc;
  228. rc = s390pci_load(&data, req, offset);
  229. if (!rc) {
  230. data = data << ((8 - len) * 8);
  231. data = le64_to_cpu(data);
  232. *val = (u32) data;
  233. } else
  234. *val = 0xffffffff;
  235. return rc;
  236. }
  237. static int zpci_cfg_store(struct zpci_dev *zdev, int offset, u32 val, u8 len)
  238. {
  239. u64 req = ZPCI_CREATE_REQ(zdev->fh, ZPCI_PCIAS_CFGSPC, len);
  240. u64 data = val;
  241. int rc;
  242. data = cpu_to_le64(data);
  243. data = data >> ((8 - len) * 8);
  244. rc = s390pci_store(data, req, offset);
  245. return rc;
  246. }
  247. void synchronize_irq(unsigned int irq)
  248. {
  249. /*
  250. * Not needed, the handler is protected by a lock and IRQs that occur
  251. * after the handler is deleted are just NOPs.
  252. */
  253. }
  254. EXPORT_SYMBOL_GPL(synchronize_irq);
  255. void enable_irq(unsigned int irq)
  256. {
  257. struct msi_desc *msi = irq_get_msi_desc(irq);
  258. zpci_msi_set_mask_bits(msi, 1, 0);
  259. }
  260. EXPORT_SYMBOL_GPL(enable_irq);
  261. void disable_irq(unsigned int irq)
  262. {
  263. struct msi_desc *msi = irq_get_msi_desc(irq);
  264. zpci_msi_set_mask_bits(msi, 1, 1);
  265. }
  266. EXPORT_SYMBOL_GPL(disable_irq);
  267. void disable_irq_nosync(unsigned int irq)
  268. {
  269. disable_irq(irq);
  270. }
  271. EXPORT_SYMBOL_GPL(disable_irq_nosync);
  272. unsigned long probe_irq_on(void)
  273. {
  274. return 0;
  275. }
  276. EXPORT_SYMBOL_GPL(probe_irq_on);
  277. int probe_irq_off(unsigned long val)
  278. {
  279. return 0;
  280. }
  281. EXPORT_SYMBOL_GPL(probe_irq_off);
  282. unsigned int probe_irq_mask(unsigned long val)
  283. {
  284. return val;
  285. }
  286. EXPORT_SYMBOL_GPL(probe_irq_mask);
  287. void pcibios_fixup_bus(struct pci_bus *bus)
  288. {
  289. }
  290. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  291. resource_size_t size,
  292. resource_size_t align)
  293. {
  294. return 0;
  295. }
  296. /* combine single writes by using store-block insn */
  297. void __iowrite64_copy(void __iomem *to, const void *from, size_t count)
  298. {
  299. zpci_memcpy_toio(to, from, count);
  300. }
  301. /* Create a virtual mapping cookie for a PCI BAR */
  302. void __iomem *pci_iomap(struct pci_dev *pdev, int bar, unsigned long max)
  303. {
  304. struct zpci_dev *zdev = get_zdev(pdev);
  305. u64 addr;
  306. int idx;
  307. if ((bar & 7) != bar)
  308. return NULL;
  309. idx = zdev->bars[bar].map_idx;
  310. spin_lock(&zpci_iomap_lock);
  311. zpci_iomap_start[idx].fh = zdev->fh;
  312. zpci_iomap_start[idx].bar = bar;
  313. spin_unlock(&zpci_iomap_lock);
  314. addr = ZPCI_IOMAP_ADDR_BASE | ((u64) idx << 48);
  315. return (void __iomem *) addr;
  316. }
  317. EXPORT_SYMBOL_GPL(pci_iomap);
  318. void pci_iounmap(struct pci_dev *pdev, void __iomem *addr)
  319. {
  320. unsigned int idx;
  321. idx = (((__force u64) addr) & ~ZPCI_IOMAP_ADDR_BASE) >> 48;
  322. spin_lock(&zpci_iomap_lock);
  323. zpci_iomap_start[idx].fh = 0;
  324. zpci_iomap_start[idx].bar = 0;
  325. spin_unlock(&zpci_iomap_lock);
  326. }
  327. EXPORT_SYMBOL_GPL(pci_iounmap);
  328. static int pci_read(struct pci_bus *bus, unsigned int devfn, int where,
  329. int size, u32 *val)
  330. {
  331. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  332. int ret;
  333. if (!zdev || devfn != ZPCI_DEVFN)
  334. ret = -ENODEV;
  335. else
  336. ret = zpci_cfg_load(zdev, where, val, size);
  337. return ret;
  338. }
  339. static int pci_write(struct pci_bus *bus, unsigned int devfn, int where,
  340. int size, u32 val)
  341. {
  342. struct zpci_dev *zdev = get_zdev_by_bus(bus);
  343. int ret;
  344. if (!zdev || devfn != ZPCI_DEVFN)
  345. ret = -ENODEV;
  346. else
  347. ret = zpci_cfg_store(zdev, where, val, size);
  348. return ret;
  349. }
  350. static struct pci_ops pci_root_ops = {
  351. .read = pci_read,
  352. .write = pci_write,
  353. };
  354. /* store the last handled bit to implement fair scheduling of devices */
  355. static DEFINE_PER_CPU(unsigned long, next_sbit);
  356. static void zpci_irq_handler(void *dont, void *need)
  357. {
  358. unsigned long sbit, mbit, last = 0, start = __get_cpu_var(next_sbit);
  359. int rescan = 0, max = aisb_max;
  360. struct zdev_irq_map *imap;
  361. inc_irq_stat(IRQIO_PCI);
  362. sbit = start;
  363. scan:
  364. /* find summary_bit */
  365. for_each_set_bit_left_cont(sbit, bucket->aisb, max) {
  366. clear_bit(63 - (sbit & 63), bucket->aisb + (sbit >> 6));
  367. last = sbit;
  368. /* find vector bit */
  369. imap = bucket->imap[sbit];
  370. for_each_set_bit_left(mbit, &imap->aibv, imap->msi_vecs) {
  371. inc_irq_stat(IRQIO_MSI);
  372. clear_bit(63 - mbit, &imap->aibv);
  373. spin_lock(&imap->lock);
  374. if (imap->cb[mbit].handler)
  375. imap->cb[mbit].handler(mbit,
  376. imap->cb[mbit].data);
  377. spin_unlock(&imap->lock);
  378. }
  379. }
  380. if (rescan)
  381. goto out;
  382. /* scan the skipped bits */
  383. if (start > 0) {
  384. sbit = 0;
  385. max = start;
  386. start = 0;
  387. goto scan;
  388. }
  389. /* enable interrupts again */
  390. set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
  391. /* check again to not lose initiative */
  392. rmb();
  393. max = aisb_max;
  394. sbit = find_first_bit_left(bucket->aisb, max);
  395. if (sbit != max) {
  396. atomic_inc(&irq_retries);
  397. rescan++;
  398. goto scan;
  399. }
  400. out:
  401. /* store next device bit to scan */
  402. __get_cpu_var(next_sbit) = (++last >= aisb_max) ? 0 : last;
  403. }
  404. /* msi_vecs - number of requested interrupts, 0 place function to error state */
  405. static int zpci_setup_msi(struct pci_dev *pdev, int msi_vecs)
  406. {
  407. struct zpci_dev *zdev = get_zdev(pdev);
  408. unsigned int aisb, msi_nr;
  409. struct msi_desc *msi;
  410. int rc;
  411. /* store the number of used MSI vectors */
  412. zdev->irq_map->msi_vecs = min(msi_vecs, ZPCI_NR_MSI_VECS);
  413. spin_lock(&bucket->lock);
  414. aisb = find_first_zero_bit(bucket->alloc, PAGE_SIZE);
  415. /* alloc map exhausted? */
  416. if (aisb == PAGE_SIZE) {
  417. spin_unlock(&bucket->lock);
  418. return -EIO;
  419. }
  420. set_bit(aisb, bucket->alloc);
  421. spin_unlock(&bucket->lock);
  422. zdev->aisb = aisb;
  423. if (aisb + 1 > aisb_max)
  424. aisb_max = aisb + 1;
  425. /* wire up IRQ shortcut pointer */
  426. bucket->imap[zdev->aisb] = zdev->irq_map;
  427. pr_debug("%s: imap[%u] linked to %p\n", __func__, zdev->aisb, zdev->irq_map);
  428. /* TODO: irq number 0 wont be found if we return less than requested MSIs.
  429. * ignore it for now and fix in common code.
  430. */
  431. msi_nr = aisb << ZPCI_MSI_VEC_BITS;
  432. list_for_each_entry(msi, &pdev->msi_list, list) {
  433. rc = zpci_setup_msi_irq(zdev, msi, msi_nr,
  434. aisb << ZPCI_MSI_VEC_BITS);
  435. if (rc)
  436. return rc;
  437. msi_nr++;
  438. }
  439. rc = zpci_register_airq(zdev, aisb, (u64) &zdev->irq_map->aibv);
  440. if (rc) {
  441. clear_bit(aisb, bucket->alloc);
  442. dev_err(&pdev->dev, "register MSI failed with: %d\n", rc);
  443. return rc;
  444. }
  445. return (zdev->irq_map->msi_vecs == msi_vecs) ?
  446. 0 : zdev->irq_map->msi_vecs;
  447. }
  448. static void zpci_teardown_msi(struct pci_dev *pdev)
  449. {
  450. struct zpci_dev *zdev = get_zdev(pdev);
  451. struct msi_desc *msi;
  452. int aisb, rc;
  453. rc = zpci_unregister_airq(zdev);
  454. if (rc) {
  455. dev_err(&pdev->dev, "deregister MSI failed with: %d\n", rc);
  456. return;
  457. }
  458. msi = list_first_entry(&pdev->msi_list, struct msi_desc, list);
  459. aisb = irq_to_dev_nr(msi->irq);
  460. list_for_each_entry(msi, &pdev->msi_list, list)
  461. zpci_teardown_msi_irq(zdev, msi);
  462. clear_bit(aisb, bucket->alloc);
  463. if (aisb + 1 == aisb_max)
  464. aisb_max--;
  465. }
  466. int arch_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
  467. {
  468. pr_debug("%s: requesting %d MSI-X interrupts...", __func__, nvec);
  469. if (type != PCI_CAP_ID_MSIX && type != PCI_CAP_ID_MSI)
  470. return -EINVAL;
  471. return zpci_setup_msi(pdev, nvec);
  472. }
  473. void arch_teardown_msi_irqs(struct pci_dev *pdev)
  474. {
  475. pr_info("%s: on pdev: %p\n", __func__, pdev);
  476. zpci_teardown_msi(pdev);
  477. }
  478. static void zpci_map_resources(struct zpci_dev *zdev)
  479. {
  480. struct pci_dev *pdev = zdev->pdev;
  481. resource_size_t len;
  482. int i;
  483. for (i = 0; i < PCI_BAR_COUNT; i++) {
  484. len = pci_resource_len(pdev, i);
  485. if (!len)
  486. continue;
  487. pdev->resource[i].start = (resource_size_t) pci_iomap(pdev, i, 0);
  488. pdev->resource[i].end = pdev->resource[i].start + len - 1;
  489. pr_debug("BAR%i: -> start: %Lx end: %Lx\n",
  490. i, pdev->resource[i].start, pdev->resource[i].end);
  491. }
  492. };
  493. static void zpci_unmap_resources(struct pci_dev *pdev)
  494. {
  495. resource_size_t len;
  496. int i;
  497. for (i = 0; i < PCI_BAR_COUNT; i++) {
  498. len = pci_resource_len(pdev, i);
  499. if (!len)
  500. continue;
  501. pci_iounmap(pdev, (void *) pdev->resource[i].start);
  502. }
  503. };
  504. struct zpci_dev *zpci_alloc_device(void)
  505. {
  506. struct zpci_dev *zdev;
  507. /* Alloc memory for our private pci device data */
  508. zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
  509. if (!zdev)
  510. return ERR_PTR(-ENOMEM);
  511. /* Alloc aibv & callback space */
  512. zdev->irq_map = kmem_cache_zalloc(zdev_irq_cache, GFP_KERNEL);
  513. if (!zdev->irq_map)
  514. goto error;
  515. WARN_ON((u64) zdev->irq_map & 0xff);
  516. return zdev;
  517. error:
  518. kfree(zdev);
  519. return ERR_PTR(-ENOMEM);
  520. }
  521. void zpci_free_device(struct zpci_dev *zdev)
  522. {
  523. kmem_cache_free(zdev_irq_cache, zdev->irq_map);
  524. kfree(zdev);
  525. }
  526. /* Called on removal of pci_dev, leaves zpci and bus device */
  527. static void zpci_remove_device(struct pci_dev *pdev)
  528. {
  529. struct zpci_dev *zdev = get_zdev(pdev);
  530. dev_info(&pdev->dev, "Removing device %u\n", zdev->domain);
  531. zdev->state = ZPCI_FN_STATE_CONFIGURED;
  532. zpci_dma_exit_device(zdev);
  533. zpci_fmb_disable_device(zdev);
  534. zpci_sysfs_remove_device(&pdev->dev);
  535. zpci_unmap_resources(pdev);
  536. list_del(&zdev->entry); /* can be called from init */
  537. zdev->pdev = NULL;
  538. }
  539. static void zpci_scan_devices(void)
  540. {
  541. struct zpci_dev *zdev;
  542. mutex_lock(&zpci_list_lock);
  543. list_for_each_entry(zdev, &zpci_list, entry)
  544. if (zdev->state == ZPCI_FN_STATE_CONFIGURED)
  545. zpci_scan_device(zdev);
  546. mutex_unlock(&zpci_list_lock);
  547. }
  548. /*
  549. * Too late for any s390 specific setup, since interrupts must be set up
  550. * already which requires DMA setup too and the pci scan will access the
  551. * config space, which only works if the function handle is enabled.
  552. */
  553. int pcibios_enable_device(struct pci_dev *pdev, int mask)
  554. {
  555. struct resource *res;
  556. u16 cmd;
  557. int i;
  558. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  559. for (i = 0; i < PCI_BAR_COUNT; i++) {
  560. res = &pdev->resource[i];
  561. if (res->flags & IORESOURCE_IO)
  562. return -EINVAL;
  563. if (res->flags & IORESOURCE_MEM)
  564. cmd |= PCI_COMMAND_MEMORY;
  565. }
  566. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  567. return 0;
  568. }
  569. void pcibios_disable_device(struct pci_dev *pdev)
  570. {
  571. zpci_remove_device(pdev);
  572. pdev->sysdata = NULL;
  573. }
  574. int pcibios_add_platform_entries(struct pci_dev *pdev)
  575. {
  576. return zpci_sysfs_add_device(&pdev->dev);
  577. }
  578. int zpci_request_irq(unsigned int irq, irq_handler_t handler, void *data)
  579. {
  580. int msi_nr = irq_to_msi_nr(irq);
  581. struct zdev_irq_map *imap;
  582. struct msi_desc *msi;
  583. msi = irq_get_msi_desc(irq);
  584. if (!msi)
  585. return -EIO;
  586. imap = get_imap(irq);
  587. spin_lock_init(&imap->lock);
  588. pr_debug("%s: register handler for IRQ:MSI %d:%d\n", __func__, irq >> 6, msi_nr);
  589. imap->cb[msi_nr].handler = handler;
  590. imap->cb[msi_nr].data = data;
  591. /*
  592. * The generic MSI code returns with the interrupt disabled on the
  593. * card, using the MSI mask bits. Firmware doesn't appear to unmask
  594. * at that level, so we do it here by hand.
  595. */
  596. zpci_msi_set_mask_bits(msi, 1, 0);
  597. return 0;
  598. }
  599. void zpci_free_irq(unsigned int irq)
  600. {
  601. struct zdev_irq_map *imap = get_imap(irq);
  602. int msi_nr = irq_to_msi_nr(irq);
  603. unsigned long flags;
  604. pr_debug("%s: for irq: %d\n", __func__, irq);
  605. spin_lock_irqsave(&imap->lock, flags);
  606. imap->cb[msi_nr].handler = NULL;
  607. imap->cb[msi_nr].data = NULL;
  608. spin_unlock_irqrestore(&imap->lock, flags);
  609. }
  610. int request_irq(unsigned int irq, irq_handler_t handler,
  611. unsigned long irqflags, const char *devname, void *dev_id)
  612. {
  613. pr_debug("%s: irq: %d handler: %p flags: %lx dev: %s\n",
  614. __func__, irq, handler, irqflags, devname);
  615. return zpci_request_irq(irq, handler, dev_id);
  616. }
  617. EXPORT_SYMBOL_GPL(request_irq);
  618. void free_irq(unsigned int irq, void *dev_id)
  619. {
  620. zpci_free_irq(irq);
  621. }
  622. EXPORT_SYMBOL_GPL(free_irq);
  623. static int __init zpci_irq_init(void)
  624. {
  625. int cpu, rc;
  626. bucket = kzalloc(sizeof(*bucket), GFP_KERNEL);
  627. if (!bucket)
  628. return -ENOMEM;
  629. bucket->aisb = (unsigned long *) get_zeroed_page(GFP_KERNEL);
  630. if (!bucket->aisb) {
  631. rc = -ENOMEM;
  632. goto out_aisb;
  633. }
  634. bucket->alloc = (unsigned long *) get_zeroed_page(GFP_KERNEL);
  635. if (!bucket->alloc) {
  636. rc = -ENOMEM;
  637. goto out_alloc;
  638. }
  639. isc_register(PCI_ISC);
  640. zpci_irq_si = s390_register_adapter_interrupt(&zpci_irq_handler, NULL, PCI_ISC);
  641. if (IS_ERR(zpci_irq_si)) {
  642. rc = PTR_ERR(zpci_irq_si);
  643. zpci_irq_si = NULL;
  644. goto out_ai;
  645. }
  646. for_each_online_cpu(cpu)
  647. per_cpu(next_sbit, cpu) = 0;
  648. spin_lock_init(&bucket->lock);
  649. /* set summary to 1 to be called every time for the ISC */
  650. *zpci_irq_si = 1;
  651. set_irq_ctrl(SIC_IRQ_MODE_SINGLE, NULL, PCI_ISC);
  652. return 0;
  653. out_ai:
  654. isc_unregister(PCI_ISC);
  655. free_page((unsigned long) bucket->alloc);
  656. out_alloc:
  657. free_page((unsigned long) bucket->aisb);
  658. out_aisb:
  659. kfree(bucket);
  660. return rc;
  661. }
  662. static void zpci_irq_exit(void)
  663. {
  664. free_page((unsigned long) bucket->alloc);
  665. free_page((unsigned long) bucket->aisb);
  666. s390_unregister_adapter_interrupt(zpci_irq_si, PCI_ISC);
  667. isc_unregister(PCI_ISC);
  668. kfree(bucket);
  669. }
  670. void zpci_debug_info(struct zpci_dev *zdev, struct seq_file *m)
  671. {
  672. if (!zdev)
  673. return;
  674. seq_printf(m, "global irq retries: %u\n", atomic_read(&irq_retries));
  675. seq_printf(m, "aibv[0]:%016lx aibv[1]:%016lx aisb:%016lx\n",
  676. get_imap(0)->aibv, get_imap(1)->aibv, *bucket->aisb);
  677. }
  678. static struct resource *zpci_alloc_bus_resource(unsigned long start, unsigned long size,
  679. unsigned long flags, int domain)
  680. {
  681. struct resource *r;
  682. char *name;
  683. int rc;
  684. r = kzalloc(sizeof(*r), GFP_KERNEL);
  685. if (!r)
  686. return ERR_PTR(-ENOMEM);
  687. r->start = start;
  688. r->end = r->start + size - 1;
  689. r->flags = flags;
  690. r->parent = &iomem_resource;
  691. name = kmalloc(18, GFP_KERNEL);
  692. if (!name) {
  693. kfree(r);
  694. return ERR_PTR(-ENOMEM);
  695. }
  696. sprintf(name, "PCI Bus: %04x:%02x", domain, ZPCI_BUS_NR);
  697. r->name = name;
  698. rc = request_resource(&iomem_resource, r);
  699. if (rc)
  700. pr_debug("request resource %pR failed\n", r);
  701. return r;
  702. }
  703. static int zpci_alloc_iomap(struct zpci_dev *zdev)
  704. {
  705. int entry;
  706. spin_lock(&zpci_iomap_lock);
  707. entry = find_first_zero_bit(zpci_iomap, ZPCI_IOMAP_MAX_ENTRIES);
  708. if (entry == ZPCI_IOMAP_MAX_ENTRIES) {
  709. spin_unlock(&zpci_iomap_lock);
  710. return -ENOSPC;
  711. }
  712. set_bit(entry, zpci_iomap);
  713. spin_unlock(&zpci_iomap_lock);
  714. return entry;
  715. }
  716. static void zpci_free_iomap(struct zpci_dev *zdev, int entry)
  717. {
  718. spin_lock(&zpci_iomap_lock);
  719. memset(&zpci_iomap_start[entry], 0, sizeof(struct zpci_iomap_entry));
  720. clear_bit(entry, zpci_iomap);
  721. spin_unlock(&zpci_iomap_lock);
  722. }
  723. int pcibios_add_device(struct pci_dev *pdev)
  724. {
  725. struct zpci_dev *zdev = get_zdev(pdev);
  726. zpci_debug_init_device(zdev);
  727. zpci_fmb_enable_device(zdev);
  728. zpci_map_resources(zdev);
  729. return 0;
  730. }
  731. static int zpci_create_device_bus(struct zpci_dev *zdev)
  732. {
  733. struct resource *res;
  734. LIST_HEAD(resources);
  735. int i;
  736. /* allocate mapping entry for each used bar */
  737. for (i = 0; i < PCI_BAR_COUNT; i++) {
  738. unsigned long addr, size, flags;
  739. int entry;
  740. if (!zdev->bars[i].size)
  741. continue;
  742. entry = zpci_alloc_iomap(zdev);
  743. if (entry < 0)
  744. return entry;
  745. zdev->bars[i].map_idx = entry;
  746. /* only MMIO is supported */
  747. flags = IORESOURCE_MEM;
  748. if (zdev->bars[i].val & 8)
  749. flags |= IORESOURCE_PREFETCH;
  750. if (zdev->bars[i].val & 4)
  751. flags |= IORESOURCE_MEM_64;
  752. addr = ZPCI_IOMAP_ADDR_BASE + ((u64) entry << 48);
  753. size = 1UL << zdev->bars[i].size;
  754. res = zpci_alloc_bus_resource(addr, size, flags, zdev->domain);
  755. if (IS_ERR(res)) {
  756. zpci_free_iomap(zdev, entry);
  757. return PTR_ERR(res);
  758. }
  759. pci_add_resource(&resources, res);
  760. }
  761. zdev->bus = pci_create_root_bus(NULL, ZPCI_BUS_NR, &pci_root_ops,
  762. zdev, &resources);
  763. if (!zdev->bus)
  764. return -EIO;
  765. zdev->bus->max_bus_speed = zdev->max_bus_speed;
  766. return 0;
  767. }
  768. static int zpci_alloc_domain(struct zpci_dev *zdev)
  769. {
  770. spin_lock(&zpci_domain_lock);
  771. zdev->domain = find_first_zero_bit(zpci_domain, ZPCI_NR_DEVICES);
  772. if (zdev->domain == ZPCI_NR_DEVICES) {
  773. spin_unlock(&zpci_domain_lock);
  774. return -ENOSPC;
  775. }
  776. set_bit(zdev->domain, zpci_domain);
  777. spin_unlock(&zpci_domain_lock);
  778. return 0;
  779. }
  780. static void zpci_free_domain(struct zpci_dev *zdev)
  781. {
  782. spin_lock(&zpci_domain_lock);
  783. clear_bit(zdev->domain, zpci_domain);
  784. spin_unlock(&zpci_domain_lock);
  785. }
  786. int zpci_enable_device(struct zpci_dev *zdev)
  787. {
  788. int rc;
  789. rc = clp_enable_fh(zdev, ZPCI_NR_DMA_SPACES);
  790. if (rc)
  791. goto out;
  792. pr_info("Enabled fh: 0x%x fid: 0x%x\n", zdev->fh, zdev->fid);
  793. rc = zpci_dma_init_device(zdev);
  794. if (rc)
  795. goto out_dma;
  796. return 0;
  797. out_dma:
  798. clp_disable_fh(zdev);
  799. out:
  800. return rc;
  801. }
  802. EXPORT_SYMBOL_GPL(zpci_enable_device);
  803. int zpci_disable_device(struct zpci_dev *zdev)
  804. {
  805. zpci_dma_exit_device(zdev);
  806. return clp_disable_fh(zdev);
  807. }
  808. EXPORT_SYMBOL_GPL(zpci_disable_device);
  809. int zpci_create_device(struct zpci_dev *zdev)
  810. {
  811. int rc;
  812. rc = zpci_alloc_domain(zdev);
  813. if (rc)
  814. goto out;
  815. rc = zpci_create_device_bus(zdev);
  816. if (rc)
  817. goto out_bus;
  818. mutex_lock(&zpci_list_lock);
  819. list_add_tail(&zdev->entry, &zpci_list);
  820. if (hotplug_ops)
  821. hotplug_ops->create_slot(zdev);
  822. mutex_unlock(&zpci_list_lock);
  823. if (zdev->state == ZPCI_FN_STATE_STANDBY)
  824. return 0;
  825. rc = zpci_enable_device(zdev);
  826. if (rc)
  827. goto out_start;
  828. return 0;
  829. out_start:
  830. mutex_lock(&zpci_list_lock);
  831. list_del(&zdev->entry);
  832. if (hotplug_ops)
  833. hotplug_ops->remove_slot(zdev);
  834. mutex_unlock(&zpci_list_lock);
  835. out_bus:
  836. zpci_free_domain(zdev);
  837. out:
  838. return rc;
  839. }
  840. void zpci_stop_device(struct zpci_dev *zdev)
  841. {
  842. zpci_dma_exit_device(zdev);
  843. /*
  844. * Note: SCLP disables fh via set-pci-fn so don't
  845. * do that here.
  846. */
  847. }
  848. EXPORT_SYMBOL_GPL(zpci_stop_device);
  849. int zpci_scan_device(struct zpci_dev *zdev)
  850. {
  851. zdev->pdev = pci_scan_single_device(zdev->bus, ZPCI_DEVFN);
  852. if (!zdev->pdev) {
  853. pr_err("pci_scan_single_device failed for fid: 0x%x\n",
  854. zdev->fid);
  855. goto out;
  856. }
  857. pci_bus_add_devices(zdev->bus);
  858. /* now that pdev was added to the bus mark it as used */
  859. zdev->state = ZPCI_FN_STATE_ONLINE;
  860. return 0;
  861. out:
  862. zpci_dma_exit_device(zdev);
  863. clp_disable_fh(zdev);
  864. return -EIO;
  865. }
  866. EXPORT_SYMBOL_GPL(zpci_scan_device);
  867. static inline int barsize(u8 size)
  868. {
  869. return (size) ? (1 << size) >> 10 : 0;
  870. }
  871. static int zpci_mem_init(void)
  872. {
  873. zdev_irq_cache = kmem_cache_create("PCI_IRQ_cache", sizeof(struct zdev_irq_map),
  874. L1_CACHE_BYTES, SLAB_HWCACHE_ALIGN, NULL);
  875. if (!zdev_irq_cache)
  876. goto error_zdev;
  877. zdev_fmb_cache = kmem_cache_create("PCI_FMB_cache", sizeof(struct zpci_fmb),
  878. 16, 0, NULL);
  879. if (!zdev_fmb_cache)
  880. goto error_fmb;
  881. /* TODO: use realloc */
  882. zpci_iomap_start = kzalloc(ZPCI_IOMAP_MAX_ENTRIES * sizeof(*zpci_iomap_start),
  883. GFP_KERNEL);
  884. if (!zpci_iomap_start)
  885. goto error_iomap;
  886. return 0;
  887. error_iomap:
  888. kmem_cache_destroy(zdev_fmb_cache);
  889. error_fmb:
  890. kmem_cache_destroy(zdev_irq_cache);
  891. error_zdev:
  892. return -ENOMEM;
  893. }
  894. static void zpci_mem_exit(void)
  895. {
  896. kfree(zpci_iomap_start);
  897. kmem_cache_destroy(zdev_irq_cache);
  898. kmem_cache_destroy(zdev_fmb_cache);
  899. }
  900. void zpci_register_hp_ops(struct pci_hp_callback_ops *ops)
  901. {
  902. mutex_lock(&zpci_list_lock);
  903. hotplug_ops = ops;
  904. mutex_unlock(&zpci_list_lock);
  905. }
  906. EXPORT_SYMBOL_GPL(zpci_register_hp_ops);
  907. void zpci_deregister_hp_ops(void)
  908. {
  909. mutex_lock(&zpci_list_lock);
  910. hotplug_ops = NULL;
  911. mutex_unlock(&zpci_list_lock);
  912. }
  913. EXPORT_SYMBOL_GPL(zpci_deregister_hp_ops);
  914. unsigned int s390_pci_probe;
  915. EXPORT_SYMBOL_GPL(s390_pci_probe);
  916. char * __init pcibios_setup(char *str)
  917. {
  918. if (!strcmp(str, "on")) {
  919. s390_pci_probe = 1;
  920. return NULL;
  921. }
  922. return str;
  923. }
  924. static int __init pci_base_init(void)
  925. {
  926. int rc;
  927. if (!s390_pci_probe)
  928. return 0;
  929. if (!test_facility(2) || !test_facility(69)
  930. || !test_facility(71) || !test_facility(72))
  931. return 0;
  932. pr_info("Probing PCI hardware: PCI:%d SID:%d AEN:%d\n",
  933. test_facility(69), test_facility(70),
  934. test_facility(71));
  935. rc = zpci_debug_init();
  936. if (rc)
  937. return rc;
  938. rc = zpci_mem_init();
  939. if (rc)
  940. goto out_mem;
  941. rc = zpci_msihash_init();
  942. if (rc)
  943. goto out_hash;
  944. rc = zpci_irq_init();
  945. if (rc)
  946. goto out_irq;
  947. rc = zpci_dma_init();
  948. if (rc)
  949. goto out_dma;
  950. rc = clp_find_pci_devices();
  951. if (rc)
  952. goto out_find;
  953. zpci_scan_devices();
  954. return 0;
  955. out_find:
  956. zpci_dma_exit();
  957. out_dma:
  958. zpci_irq_exit();
  959. out_irq:
  960. zpci_msihash_exit();
  961. out_hash:
  962. zpci_mem_exit();
  963. out_mem:
  964. zpci_debug_exit();
  965. return rc;
  966. }
  967. subsys_initcall(pci_base_init);