pci.c 25 KB

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