pcie-designware-ep.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Synopsys DesignWare PCIe Endpoint controller driver
  4. *
  5. * Copyright (C) 2017 Texas Instruments
  6. * Author: Kishon Vijay Abraham I <[email protected]>
  7. */
  8. #include <linux/of.h>
  9. #include <linux/platform_device.h>
  10. #include "pcie-designware.h"
  11. #include <linux/pci-epc.h>
  12. #include <linux/pci-epf.h>
  13. #include "../../pci.h"
  14. void dw_pcie_ep_linkup(struct dw_pcie_ep *ep)
  15. {
  16. struct pci_epc *epc = ep->epc;
  17. pci_epc_linkup(epc);
  18. }
  19. EXPORT_SYMBOL_GPL(dw_pcie_ep_linkup);
  20. void dw_pcie_ep_init_notify(struct dw_pcie_ep *ep)
  21. {
  22. struct pci_epc *epc = ep->epc;
  23. pci_epc_init_notify(epc);
  24. }
  25. EXPORT_SYMBOL_GPL(dw_pcie_ep_init_notify);
  26. struct dw_pcie_ep_func *
  27. dw_pcie_ep_get_func_from_ep(struct dw_pcie_ep *ep, u8 func_no)
  28. {
  29. struct dw_pcie_ep_func *ep_func;
  30. list_for_each_entry(ep_func, &ep->func_list, list) {
  31. if (ep_func->func_no == func_no)
  32. return ep_func;
  33. }
  34. return NULL;
  35. }
  36. static unsigned int dw_pcie_ep_func_select(struct dw_pcie_ep *ep, u8 func_no)
  37. {
  38. unsigned int func_offset = 0;
  39. if (ep->ops->func_conf_select)
  40. func_offset = ep->ops->func_conf_select(ep, func_no);
  41. return func_offset;
  42. }
  43. static void __dw_pcie_ep_reset_bar(struct dw_pcie *pci, u8 func_no,
  44. enum pci_barno bar, int flags)
  45. {
  46. u32 reg;
  47. unsigned int func_offset = 0;
  48. struct dw_pcie_ep *ep = &pci->ep;
  49. func_offset = dw_pcie_ep_func_select(ep, func_no);
  50. reg = func_offset + PCI_BASE_ADDRESS_0 + (4 * bar);
  51. dw_pcie_dbi_ro_wr_en(pci);
  52. dw_pcie_writel_dbi2(pci, reg, 0x0);
  53. dw_pcie_writel_dbi(pci, reg, 0x0);
  54. if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  55. dw_pcie_writel_dbi2(pci, reg + 4, 0x0);
  56. dw_pcie_writel_dbi(pci, reg + 4, 0x0);
  57. }
  58. dw_pcie_dbi_ro_wr_dis(pci);
  59. }
  60. void dw_pcie_ep_reset_bar(struct dw_pcie *pci, enum pci_barno bar)
  61. {
  62. u8 func_no, funcs;
  63. funcs = pci->ep.epc->max_functions;
  64. for (func_no = 0; func_no < funcs; func_no++)
  65. __dw_pcie_ep_reset_bar(pci, func_no, bar, 0);
  66. }
  67. EXPORT_SYMBOL_GPL(dw_pcie_ep_reset_bar);
  68. static u8 __dw_pcie_ep_find_next_cap(struct dw_pcie_ep *ep, u8 func_no,
  69. u8 cap_ptr, u8 cap)
  70. {
  71. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  72. unsigned int func_offset = 0;
  73. u8 cap_id, next_cap_ptr;
  74. u16 reg;
  75. if (!cap_ptr)
  76. return 0;
  77. func_offset = dw_pcie_ep_func_select(ep, func_no);
  78. reg = dw_pcie_readw_dbi(pci, func_offset + cap_ptr);
  79. cap_id = (reg & 0x00ff);
  80. if (cap_id > PCI_CAP_ID_MAX)
  81. return 0;
  82. if (cap_id == cap)
  83. return cap_ptr;
  84. next_cap_ptr = (reg & 0xff00) >> 8;
  85. return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
  86. }
  87. static u8 dw_pcie_ep_find_capability(struct dw_pcie_ep *ep, u8 func_no, u8 cap)
  88. {
  89. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  90. unsigned int func_offset = 0;
  91. u8 next_cap_ptr;
  92. u16 reg;
  93. func_offset = dw_pcie_ep_func_select(ep, func_no);
  94. reg = dw_pcie_readw_dbi(pci, func_offset + PCI_CAPABILITY_LIST);
  95. next_cap_ptr = (reg & 0x00ff);
  96. return __dw_pcie_ep_find_next_cap(ep, func_no, next_cap_ptr, cap);
  97. }
  98. static int dw_pcie_ep_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  99. struct pci_epf_header *hdr)
  100. {
  101. struct dw_pcie_ep *ep = epc_get_drvdata(epc);
  102. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  103. unsigned int func_offset = 0;
  104. func_offset = dw_pcie_ep_func_select(ep, func_no);
  105. dw_pcie_dbi_ro_wr_en(pci);
  106. dw_pcie_writew_dbi(pci, func_offset + PCI_VENDOR_ID, hdr->vendorid);
  107. dw_pcie_writew_dbi(pci, func_offset + PCI_DEVICE_ID, hdr->deviceid);
  108. dw_pcie_writeb_dbi(pci, func_offset + PCI_REVISION_ID, hdr->revid);
  109. dw_pcie_writeb_dbi(pci, func_offset + PCI_CLASS_PROG, hdr->progif_code);
  110. dw_pcie_writew_dbi(pci, func_offset + PCI_CLASS_DEVICE,
  111. hdr->subclass_code | hdr->baseclass_code << 8);
  112. dw_pcie_writeb_dbi(pci, func_offset + PCI_CACHE_LINE_SIZE,
  113. hdr->cache_line_size);
  114. dw_pcie_writew_dbi(pci, func_offset + PCI_SUBSYSTEM_VENDOR_ID,
  115. hdr->subsys_vendor_id);
  116. dw_pcie_writew_dbi(pci, func_offset + PCI_SUBSYSTEM_ID, hdr->subsys_id);
  117. dw_pcie_writeb_dbi(pci, func_offset + PCI_INTERRUPT_PIN,
  118. hdr->interrupt_pin);
  119. dw_pcie_dbi_ro_wr_dis(pci);
  120. return 0;
  121. }
  122. static int dw_pcie_ep_inbound_atu(struct dw_pcie_ep *ep, u8 func_no, int type,
  123. dma_addr_t cpu_addr, enum pci_barno bar)
  124. {
  125. int ret;
  126. u32 free_win;
  127. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  128. if (!ep->bar_to_atu[bar])
  129. free_win = find_first_zero_bit(ep->ib_window_map, pci->num_ib_windows);
  130. else
  131. free_win = ep->bar_to_atu[bar];
  132. if (free_win >= pci->num_ib_windows) {
  133. dev_err(pci->dev, "No free inbound window\n");
  134. return -EINVAL;
  135. }
  136. ret = dw_pcie_prog_inbound_atu(pci, func_no, free_win, type,
  137. cpu_addr, bar);
  138. if (ret < 0) {
  139. dev_err(pci->dev, "Failed to program IB window\n");
  140. return ret;
  141. }
  142. ep->bar_to_atu[bar] = free_win;
  143. set_bit(free_win, ep->ib_window_map);
  144. return 0;
  145. }
  146. static int dw_pcie_ep_outbound_atu(struct dw_pcie_ep *ep, u8 func_no,
  147. phys_addr_t phys_addr,
  148. u64 pci_addr, size_t size)
  149. {
  150. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  151. u32 free_win;
  152. int ret;
  153. free_win = find_first_zero_bit(ep->ob_window_map, pci->num_ob_windows);
  154. if (free_win >= pci->num_ob_windows) {
  155. dev_err(pci->dev, "No free outbound window\n");
  156. return -EINVAL;
  157. }
  158. ret = dw_pcie_prog_ep_outbound_atu(pci, func_no, free_win, PCIE_ATU_TYPE_MEM,
  159. phys_addr, pci_addr, size);
  160. if (ret)
  161. return ret;
  162. set_bit(free_win, ep->ob_window_map);
  163. ep->outbound_addr[free_win] = phys_addr;
  164. return 0;
  165. }
  166. static void dw_pcie_ep_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  167. struct pci_epf_bar *epf_bar)
  168. {
  169. struct dw_pcie_ep *ep = epc_get_drvdata(epc);
  170. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  171. enum pci_barno bar = epf_bar->barno;
  172. u32 atu_index = ep->bar_to_atu[bar];
  173. __dw_pcie_ep_reset_bar(pci, func_no, bar, epf_bar->flags);
  174. dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_IB, atu_index);
  175. clear_bit(atu_index, ep->ib_window_map);
  176. ep->epf_bar[bar] = NULL;
  177. ep->bar_to_atu[bar] = 0;
  178. }
  179. static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  180. struct pci_epf_bar *epf_bar)
  181. {
  182. struct dw_pcie_ep *ep = epc_get_drvdata(epc);
  183. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  184. enum pci_barno bar = epf_bar->barno;
  185. size_t size = epf_bar->size;
  186. int flags = epf_bar->flags;
  187. unsigned int func_offset = 0;
  188. int ret, type;
  189. u32 reg;
  190. func_offset = dw_pcie_ep_func_select(ep, func_no);
  191. reg = PCI_BASE_ADDRESS_0 + (4 * bar) + func_offset;
  192. if (!(flags & PCI_BASE_ADDRESS_SPACE))
  193. type = PCIE_ATU_TYPE_MEM;
  194. else
  195. type = PCIE_ATU_TYPE_IO;
  196. ret = dw_pcie_ep_inbound_atu(ep, func_no, type, epf_bar->phys_addr, bar);
  197. if (ret)
  198. return ret;
  199. if (ep->epf_bar[bar])
  200. return 0;
  201. dw_pcie_dbi_ro_wr_en(pci);
  202. dw_pcie_writel_dbi2(pci, reg, lower_32_bits(size - 1));
  203. dw_pcie_writel_dbi(pci, reg, flags);
  204. if (flags & PCI_BASE_ADDRESS_MEM_TYPE_64) {
  205. dw_pcie_writel_dbi2(pci, reg + 4, upper_32_bits(size - 1));
  206. dw_pcie_writel_dbi(pci, reg + 4, 0);
  207. }
  208. ep->epf_bar[bar] = epf_bar;
  209. dw_pcie_dbi_ro_wr_dis(pci);
  210. return 0;
  211. }
  212. static int dw_pcie_find_index(struct dw_pcie_ep *ep, phys_addr_t addr,
  213. u32 *atu_index)
  214. {
  215. u32 index;
  216. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  217. for (index = 0; index < pci->num_ob_windows; index++) {
  218. if (ep->outbound_addr[index] != addr)
  219. continue;
  220. *atu_index = index;
  221. return 0;
  222. }
  223. return -EINVAL;
  224. }
  225. static void dw_pcie_ep_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  226. phys_addr_t addr)
  227. {
  228. int ret;
  229. u32 atu_index;
  230. struct dw_pcie_ep *ep = epc_get_drvdata(epc);
  231. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  232. ret = dw_pcie_find_index(ep, addr, &atu_index);
  233. if (ret < 0)
  234. return;
  235. dw_pcie_disable_atu(pci, PCIE_ATU_REGION_DIR_OB, atu_index);
  236. clear_bit(atu_index, ep->ob_window_map);
  237. }
  238. static int dw_pcie_ep_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  239. phys_addr_t addr, u64 pci_addr, size_t size)
  240. {
  241. int ret;
  242. struct dw_pcie_ep *ep = epc_get_drvdata(epc);
  243. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  244. ret = dw_pcie_ep_outbound_atu(ep, func_no, addr, pci_addr, size);
  245. if (ret) {
  246. dev_err(pci->dev, "Failed to enable address\n");
  247. return ret;
  248. }
  249. return 0;
  250. }
  251. static int dw_pcie_ep_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
  252. {
  253. struct dw_pcie_ep *ep = epc_get_drvdata(epc);
  254. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  255. u32 val, reg;
  256. unsigned int func_offset = 0;
  257. struct dw_pcie_ep_func *ep_func;
  258. ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
  259. if (!ep_func || !ep_func->msi_cap)
  260. return -EINVAL;
  261. func_offset = dw_pcie_ep_func_select(ep, func_no);
  262. reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
  263. val = dw_pcie_readw_dbi(pci, reg);
  264. if (!(val & PCI_MSI_FLAGS_ENABLE))
  265. return -EINVAL;
  266. val = (val & PCI_MSI_FLAGS_QSIZE) >> 4;
  267. return val;
  268. }
  269. static int dw_pcie_ep_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  270. u8 interrupts)
  271. {
  272. struct dw_pcie_ep *ep = epc_get_drvdata(epc);
  273. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  274. u32 val, reg;
  275. unsigned int func_offset = 0;
  276. struct dw_pcie_ep_func *ep_func;
  277. ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
  278. if (!ep_func || !ep_func->msi_cap)
  279. return -EINVAL;
  280. func_offset = dw_pcie_ep_func_select(ep, func_no);
  281. reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
  282. val = dw_pcie_readw_dbi(pci, reg);
  283. val &= ~PCI_MSI_FLAGS_QMASK;
  284. val |= (interrupts << 1) & PCI_MSI_FLAGS_QMASK;
  285. dw_pcie_dbi_ro_wr_en(pci);
  286. dw_pcie_writew_dbi(pci, reg, val);
  287. dw_pcie_dbi_ro_wr_dis(pci);
  288. return 0;
  289. }
  290. static int dw_pcie_ep_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
  291. {
  292. struct dw_pcie_ep *ep = epc_get_drvdata(epc);
  293. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  294. u32 val, reg;
  295. unsigned int func_offset = 0;
  296. struct dw_pcie_ep_func *ep_func;
  297. ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
  298. if (!ep_func || !ep_func->msix_cap)
  299. return -EINVAL;
  300. func_offset = dw_pcie_ep_func_select(ep, func_no);
  301. reg = ep_func->msix_cap + func_offset + PCI_MSIX_FLAGS;
  302. val = dw_pcie_readw_dbi(pci, reg);
  303. if (!(val & PCI_MSIX_FLAGS_ENABLE))
  304. return -EINVAL;
  305. val &= PCI_MSIX_FLAGS_QSIZE;
  306. return val;
  307. }
  308. static int dw_pcie_ep_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  309. u16 interrupts, enum pci_barno bir, u32 offset)
  310. {
  311. struct dw_pcie_ep *ep = epc_get_drvdata(epc);
  312. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  313. u32 val, reg;
  314. unsigned int func_offset = 0;
  315. struct dw_pcie_ep_func *ep_func;
  316. ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
  317. if (!ep_func || !ep_func->msix_cap)
  318. return -EINVAL;
  319. dw_pcie_dbi_ro_wr_en(pci);
  320. func_offset = dw_pcie_ep_func_select(ep, func_no);
  321. reg = ep_func->msix_cap + func_offset + PCI_MSIX_FLAGS;
  322. val = dw_pcie_readw_dbi(pci, reg);
  323. val &= ~PCI_MSIX_FLAGS_QSIZE;
  324. val |= interrupts;
  325. dw_pcie_writew_dbi(pci, reg, val);
  326. reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
  327. val = offset | bir;
  328. dw_pcie_writel_dbi(pci, reg, val);
  329. reg = ep_func->msix_cap + func_offset + PCI_MSIX_PBA;
  330. val = (offset + (interrupts * PCI_MSIX_ENTRY_SIZE)) | bir;
  331. dw_pcie_writel_dbi(pci, reg, val);
  332. dw_pcie_dbi_ro_wr_dis(pci);
  333. return 0;
  334. }
  335. static int dw_pcie_ep_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
  336. enum pci_epc_irq_type type, u16 interrupt_num)
  337. {
  338. struct dw_pcie_ep *ep = epc_get_drvdata(epc);
  339. if (!ep->ops->raise_irq)
  340. return -EINVAL;
  341. return ep->ops->raise_irq(ep, func_no, type, interrupt_num);
  342. }
  343. static void dw_pcie_ep_stop(struct pci_epc *epc)
  344. {
  345. struct dw_pcie_ep *ep = epc_get_drvdata(epc);
  346. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  347. dw_pcie_stop_link(pci);
  348. }
  349. static int dw_pcie_ep_start(struct pci_epc *epc)
  350. {
  351. struct dw_pcie_ep *ep = epc_get_drvdata(epc);
  352. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  353. return dw_pcie_start_link(pci);
  354. }
  355. static const struct pci_epc_features*
  356. dw_pcie_ep_get_features(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
  357. {
  358. struct dw_pcie_ep *ep = epc_get_drvdata(epc);
  359. if (!ep->ops->get_features)
  360. return NULL;
  361. return ep->ops->get_features(ep);
  362. }
  363. static const struct pci_epc_ops epc_ops = {
  364. .write_header = dw_pcie_ep_write_header,
  365. .set_bar = dw_pcie_ep_set_bar,
  366. .clear_bar = dw_pcie_ep_clear_bar,
  367. .map_addr = dw_pcie_ep_map_addr,
  368. .unmap_addr = dw_pcie_ep_unmap_addr,
  369. .set_msi = dw_pcie_ep_set_msi,
  370. .get_msi = dw_pcie_ep_get_msi,
  371. .set_msix = dw_pcie_ep_set_msix,
  372. .get_msix = dw_pcie_ep_get_msix,
  373. .raise_irq = dw_pcie_ep_raise_irq,
  374. .start = dw_pcie_ep_start,
  375. .stop = dw_pcie_ep_stop,
  376. .get_features = dw_pcie_ep_get_features,
  377. };
  378. int dw_pcie_ep_raise_legacy_irq(struct dw_pcie_ep *ep, u8 func_no)
  379. {
  380. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  381. struct device *dev = pci->dev;
  382. dev_err(dev, "EP cannot trigger legacy IRQs\n");
  383. return -EINVAL;
  384. }
  385. EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_legacy_irq);
  386. int dw_pcie_ep_raise_msi_irq(struct dw_pcie_ep *ep, u8 func_no,
  387. u8 interrupt_num)
  388. {
  389. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  390. struct dw_pcie_ep_func *ep_func;
  391. struct pci_epc *epc = ep->epc;
  392. unsigned int aligned_offset;
  393. unsigned int func_offset = 0;
  394. u16 msg_ctrl, msg_data;
  395. u32 msg_addr_lower, msg_addr_upper, reg;
  396. u64 msg_addr;
  397. bool has_upper;
  398. int ret;
  399. ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
  400. if (!ep_func || !ep_func->msi_cap)
  401. return -EINVAL;
  402. func_offset = dw_pcie_ep_func_select(ep, func_no);
  403. /* Raise MSI per the PCI Local Bus Specification Revision 3.0, 6.8.1. */
  404. reg = ep_func->msi_cap + func_offset + PCI_MSI_FLAGS;
  405. msg_ctrl = dw_pcie_readw_dbi(pci, reg);
  406. has_upper = !!(msg_ctrl & PCI_MSI_FLAGS_64BIT);
  407. reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_LO;
  408. msg_addr_lower = dw_pcie_readl_dbi(pci, reg);
  409. if (has_upper) {
  410. reg = ep_func->msi_cap + func_offset + PCI_MSI_ADDRESS_HI;
  411. msg_addr_upper = dw_pcie_readl_dbi(pci, reg);
  412. reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_64;
  413. msg_data = dw_pcie_readw_dbi(pci, reg);
  414. } else {
  415. msg_addr_upper = 0;
  416. reg = ep_func->msi_cap + func_offset + PCI_MSI_DATA_32;
  417. msg_data = dw_pcie_readw_dbi(pci, reg);
  418. }
  419. aligned_offset = msg_addr_lower & (epc->mem->window.page_size - 1);
  420. msg_addr = ((u64)msg_addr_upper) << 32 |
  421. (msg_addr_lower & ~aligned_offset);
  422. ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
  423. epc->mem->window.page_size);
  424. if (ret)
  425. return ret;
  426. writel(msg_data | (interrupt_num - 1), ep->msi_mem + aligned_offset);
  427. dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
  428. return 0;
  429. }
  430. EXPORT_SYMBOL_GPL(dw_pcie_ep_raise_msi_irq);
  431. int dw_pcie_ep_raise_msix_irq_doorbell(struct dw_pcie_ep *ep, u8 func_no,
  432. u16 interrupt_num)
  433. {
  434. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  435. struct dw_pcie_ep_func *ep_func;
  436. u32 msg_data;
  437. ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
  438. if (!ep_func || !ep_func->msix_cap)
  439. return -EINVAL;
  440. msg_data = (func_no << PCIE_MSIX_DOORBELL_PF_SHIFT) |
  441. (interrupt_num - 1);
  442. dw_pcie_writel_dbi(pci, PCIE_MSIX_DOORBELL, msg_data);
  443. return 0;
  444. }
  445. int dw_pcie_ep_raise_msix_irq(struct dw_pcie_ep *ep, u8 func_no,
  446. u16 interrupt_num)
  447. {
  448. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  449. struct dw_pcie_ep_func *ep_func;
  450. struct pci_epf_msix_tbl *msix_tbl;
  451. struct pci_epc *epc = ep->epc;
  452. unsigned int func_offset = 0;
  453. u32 reg, msg_data, vec_ctrl;
  454. unsigned int aligned_offset;
  455. u32 tbl_offset;
  456. u64 msg_addr;
  457. int ret;
  458. u8 bir;
  459. ep_func = dw_pcie_ep_get_func_from_ep(ep, func_no);
  460. if (!ep_func || !ep_func->msix_cap)
  461. return -EINVAL;
  462. func_offset = dw_pcie_ep_func_select(ep, func_no);
  463. reg = ep_func->msix_cap + func_offset + PCI_MSIX_TABLE;
  464. tbl_offset = dw_pcie_readl_dbi(pci, reg);
  465. bir = (tbl_offset & PCI_MSIX_TABLE_BIR);
  466. tbl_offset &= PCI_MSIX_TABLE_OFFSET;
  467. msix_tbl = ep->epf_bar[bir]->addr + tbl_offset;
  468. msg_addr = msix_tbl[(interrupt_num - 1)].msg_addr;
  469. msg_data = msix_tbl[(interrupt_num - 1)].msg_data;
  470. vec_ctrl = msix_tbl[(interrupt_num - 1)].vector_ctrl;
  471. if (vec_ctrl & PCI_MSIX_ENTRY_CTRL_MASKBIT) {
  472. dev_dbg(pci->dev, "MSI-X entry ctrl set\n");
  473. return -EPERM;
  474. }
  475. aligned_offset = msg_addr & (epc->mem->window.page_size - 1);
  476. ret = dw_pcie_ep_map_addr(epc, func_no, 0, ep->msi_mem_phys, msg_addr,
  477. epc->mem->window.page_size);
  478. if (ret)
  479. return ret;
  480. writel(msg_data, ep->msi_mem + aligned_offset);
  481. dw_pcie_ep_unmap_addr(epc, func_no, 0, ep->msi_mem_phys);
  482. return 0;
  483. }
  484. void dw_pcie_ep_exit(struct dw_pcie_ep *ep)
  485. {
  486. struct pci_epc *epc = ep->epc;
  487. pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
  488. epc->mem->window.page_size);
  489. pci_epc_mem_exit(epc);
  490. }
  491. static unsigned int dw_pcie_ep_find_ext_capability(struct dw_pcie *pci, int cap)
  492. {
  493. u32 header;
  494. int pos = PCI_CFG_SPACE_SIZE;
  495. while (pos) {
  496. header = dw_pcie_readl_dbi(pci, pos);
  497. if (PCI_EXT_CAP_ID(header) == cap)
  498. return pos;
  499. pos = PCI_EXT_CAP_NEXT(header);
  500. if (!pos)
  501. break;
  502. }
  503. return 0;
  504. }
  505. int dw_pcie_ep_init_complete(struct dw_pcie_ep *ep)
  506. {
  507. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  508. unsigned int offset;
  509. unsigned int nbars;
  510. u8 hdr_type;
  511. u32 reg;
  512. int i;
  513. hdr_type = dw_pcie_readb_dbi(pci, PCI_HEADER_TYPE) &
  514. PCI_HEADER_TYPE_MASK;
  515. if (hdr_type != PCI_HEADER_TYPE_NORMAL) {
  516. dev_err(pci->dev,
  517. "PCIe controller is not set to EP mode (hdr_type:0x%x)!\n",
  518. hdr_type);
  519. return -EIO;
  520. }
  521. offset = dw_pcie_ep_find_ext_capability(pci, PCI_EXT_CAP_ID_REBAR);
  522. dw_pcie_dbi_ro_wr_en(pci);
  523. if (offset) {
  524. reg = dw_pcie_readl_dbi(pci, offset + PCI_REBAR_CTRL);
  525. nbars = (reg & PCI_REBAR_CTRL_NBAR_MASK) >>
  526. PCI_REBAR_CTRL_NBAR_SHIFT;
  527. for (i = 0; i < nbars; i++, offset += PCI_REBAR_CTRL)
  528. dw_pcie_writel_dbi(pci, offset + PCI_REBAR_CAP, 0x0);
  529. }
  530. dw_pcie_setup(pci);
  531. dw_pcie_dbi_ro_wr_dis(pci);
  532. return 0;
  533. }
  534. EXPORT_SYMBOL_GPL(dw_pcie_ep_init_complete);
  535. int dw_pcie_ep_init(struct dw_pcie_ep *ep)
  536. {
  537. int ret;
  538. void *addr;
  539. u8 func_no;
  540. struct resource *res;
  541. struct pci_epc *epc;
  542. struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
  543. struct device *dev = pci->dev;
  544. struct platform_device *pdev = to_platform_device(dev);
  545. struct device_node *np = dev->of_node;
  546. const struct pci_epc_features *epc_features;
  547. struct dw_pcie_ep_func *ep_func;
  548. INIT_LIST_HEAD(&ep->func_list);
  549. if (!pci->dbi_base) {
  550. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi");
  551. pci->dbi_base = devm_pci_remap_cfg_resource(dev, res);
  552. if (IS_ERR(pci->dbi_base))
  553. return PTR_ERR(pci->dbi_base);
  554. }
  555. if (!pci->dbi_base2) {
  556. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbi2");
  557. if (!res) {
  558. pci->dbi_base2 = pci->dbi_base + SZ_4K;
  559. } else {
  560. pci->dbi_base2 = devm_pci_remap_cfg_resource(dev, res);
  561. if (IS_ERR(pci->dbi_base2))
  562. return PTR_ERR(pci->dbi_base2);
  563. }
  564. }
  565. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
  566. if (!res)
  567. return -EINVAL;
  568. ep->phys_base = res->start;
  569. ep->addr_size = resource_size(res);
  570. dw_pcie_version_detect(pci);
  571. dw_pcie_iatu_detect(pci);
  572. ep->ib_window_map = devm_bitmap_zalloc(dev, pci->num_ib_windows,
  573. GFP_KERNEL);
  574. if (!ep->ib_window_map)
  575. return -ENOMEM;
  576. ep->ob_window_map = devm_bitmap_zalloc(dev, pci->num_ob_windows,
  577. GFP_KERNEL);
  578. if (!ep->ob_window_map)
  579. return -ENOMEM;
  580. addr = devm_kcalloc(dev, pci->num_ob_windows, sizeof(phys_addr_t),
  581. GFP_KERNEL);
  582. if (!addr)
  583. return -ENOMEM;
  584. ep->outbound_addr = addr;
  585. if (pci->link_gen < 1)
  586. pci->link_gen = of_pci_get_max_link_speed(np);
  587. epc = devm_pci_epc_create(dev, &epc_ops);
  588. if (IS_ERR(epc)) {
  589. dev_err(dev, "Failed to create epc device\n");
  590. return PTR_ERR(epc);
  591. }
  592. ep->epc = epc;
  593. epc_set_drvdata(epc, ep);
  594. ret = of_property_read_u8(np, "max-functions", &epc->max_functions);
  595. if (ret < 0)
  596. epc->max_functions = 1;
  597. for (func_no = 0; func_no < epc->max_functions; func_no++) {
  598. ep_func = devm_kzalloc(dev, sizeof(*ep_func), GFP_KERNEL);
  599. if (!ep_func)
  600. return -ENOMEM;
  601. ep_func->func_no = func_no;
  602. ep_func->msi_cap = dw_pcie_ep_find_capability(ep, func_no,
  603. PCI_CAP_ID_MSI);
  604. ep_func->msix_cap = dw_pcie_ep_find_capability(ep, func_no,
  605. PCI_CAP_ID_MSIX);
  606. list_add_tail(&ep_func->list, &ep->func_list);
  607. }
  608. if (ep->ops->ep_init)
  609. ep->ops->ep_init(ep);
  610. ret = pci_epc_mem_init(epc, ep->phys_base, ep->addr_size,
  611. ep->page_size);
  612. if (ret < 0) {
  613. dev_err(dev, "Failed to initialize address space\n");
  614. return ret;
  615. }
  616. ep->msi_mem = pci_epc_mem_alloc_addr(epc, &ep->msi_mem_phys,
  617. epc->mem->window.page_size);
  618. if (!ep->msi_mem) {
  619. ret = -ENOMEM;
  620. dev_err(dev, "Failed to reserve memory for MSI/MSI-X\n");
  621. goto err_exit_epc_mem;
  622. }
  623. if (ep->ops->get_features) {
  624. epc_features = ep->ops->get_features(ep);
  625. if (epc_features->core_init_notifier)
  626. return 0;
  627. }
  628. ret = dw_pcie_ep_init_complete(ep);
  629. if (ret)
  630. goto err_free_epc_mem;
  631. return 0;
  632. err_free_epc_mem:
  633. pci_epc_mem_free_addr(epc, ep->msi_mem_phys, ep->msi_mem,
  634. epc->mem->window.page_size);
  635. err_exit_epc_mem:
  636. pci_epc_mem_exit(epc);
  637. return ret;
  638. }
  639. EXPORT_SYMBOL_GPL(dw_pcie_ep_init);