cdnsp-mem.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Cadence CDNSP DRD Driver.
  4. *
  5. * Copyright (C) 2020 Cadence.
  6. *
  7. * Author: Pawel Laszczak <pawell@cadence.com>
  8. *
  9. * Code based on Linux XHCI driver.
  10. * Origin: Copyright (C) 2008 Intel Corp.
  11. */
  12. #include <linux/dma-mapping.h>
  13. #include <linux/dmapool.h>
  14. #include <linux/slab.h>
  15. #include <linux/usb.h>
  16. #include "cdnsp-gadget.h"
  17. #include "cdnsp-trace.h"
  18. static void cdnsp_free_stream_info(struct cdnsp_device *pdev,
  19. struct cdnsp_ep *pep);
  20. /*
  21. * Allocates a generic ring segment from the ring pool, sets the dma address,
  22. * initializes the segment to zero, and sets the private next pointer to NULL.
  23. *
  24. * "All components of all Command and Transfer TRBs shall be initialized to '0'"
  25. */
  26. static struct cdnsp_segment *cdnsp_segment_alloc(struct cdnsp_device *pdev,
  27. unsigned int cycle_state,
  28. unsigned int max_packet,
  29. gfp_t flags)
  30. {
  31. struct cdnsp_segment *seg;
  32. dma_addr_t dma;
  33. int i;
  34. seg = kzalloc_obj(*seg, flags);
  35. if (!seg)
  36. return NULL;
  37. seg->trbs = dma_pool_zalloc(pdev->segment_pool, flags, &dma);
  38. if (!seg->trbs) {
  39. kfree(seg);
  40. return NULL;
  41. }
  42. if (max_packet) {
  43. seg->bounce_buf = kzalloc(max_packet, flags | GFP_DMA);
  44. if (!seg->bounce_buf)
  45. goto free_dma;
  46. }
  47. /* If the cycle state is 0, set the cycle bit to 1 for all the TRBs. */
  48. if (cycle_state == 0) {
  49. for (i = 0; i < TRBS_PER_SEGMENT; i++)
  50. seg->trbs[i].link.control |= cpu_to_le32(TRB_CYCLE);
  51. }
  52. seg->dma = dma;
  53. seg->next = NULL;
  54. return seg;
  55. free_dma:
  56. dma_pool_free(pdev->segment_pool, seg->trbs, dma);
  57. kfree(seg);
  58. return NULL;
  59. }
  60. static void cdnsp_segment_free(struct cdnsp_device *pdev,
  61. struct cdnsp_segment *seg)
  62. {
  63. if (seg->trbs)
  64. dma_pool_free(pdev->segment_pool, seg->trbs, seg->dma);
  65. kfree(seg->bounce_buf);
  66. kfree(seg);
  67. }
  68. static void cdnsp_free_segments_for_ring(struct cdnsp_device *pdev,
  69. struct cdnsp_segment *first)
  70. {
  71. struct cdnsp_segment *seg;
  72. seg = first->next;
  73. while (seg != first) {
  74. struct cdnsp_segment *next = seg->next;
  75. cdnsp_segment_free(pdev, seg);
  76. seg = next;
  77. }
  78. cdnsp_segment_free(pdev, first);
  79. }
  80. /*
  81. * Make the prev segment point to the next segment.
  82. *
  83. * Change the last TRB in the prev segment to be a Link TRB which points to the
  84. * DMA address of the next segment. The caller needs to set any Link TRB
  85. * related flags, such as End TRB, Toggle Cycle, and no snoop.
  86. */
  87. static void cdnsp_link_segments(struct cdnsp_device *pdev,
  88. struct cdnsp_segment *prev,
  89. struct cdnsp_segment *next,
  90. enum cdnsp_ring_type type)
  91. {
  92. struct cdnsp_link_trb *link;
  93. u32 val;
  94. if (!prev || !next)
  95. return;
  96. prev->next = next;
  97. if (type != TYPE_EVENT) {
  98. link = &prev->trbs[TRBS_PER_SEGMENT - 1].link;
  99. link->segment_ptr = cpu_to_le64(next->dma);
  100. /*
  101. * Set the last TRB in the segment to have a TRB type ID
  102. * of Link TRB
  103. */
  104. val = le32_to_cpu(link->control);
  105. val &= ~TRB_TYPE_BITMASK;
  106. val |= TRB_TYPE(TRB_LINK);
  107. link->control = cpu_to_le32(val);
  108. }
  109. }
  110. /*
  111. * Link the ring to the new segments.
  112. * Set Toggle Cycle for the new ring if needed.
  113. */
  114. static void cdnsp_link_rings(struct cdnsp_device *pdev,
  115. struct cdnsp_ring *ring,
  116. struct cdnsp_segment *first,
  117. struct cdnsp_segment *last,
  118. unsigned int num_segs)
  119. {
  120. struct cdnsp_segment *next;
  121. if (!ring || !first || !last)
  122. return;
  123. next = ring->enq_seg->next;
  124. cdnsp_link_segments(pdev, ring->enq_seg, first, ring->type);
  125. cdnsp_link_segments(pdev, last, next, ring->type);
  126. ring->num_segs += num_segs;
  127. ring->num_trbs_free += (TRBS_PER_SEGMENT - 1) * num_segs;
  128. if (ring->type != TYPE_EVENT && ring->enq_seg == ring->last_seg) {
  129. ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
  130. ~cpu_to_le32(LINK_TOGGLE);
  131. last->trbs[TRBS_PER_SEGMENT - 1].link.control |=
  132. cpu_to_le32(LINK_TOGGLE);
  133. ring->last_seg = last;
  134. }
  135. }
  136. /*
  137. * We need a radix tree for mapping physical addresses of TRBs to which stream
  138. * ID they belong to. We need to do this because the device controller won't
  139. * tell us which stream ring the TRB came from. We could store the stream ID
  140. * in an event data TRB, but that doesn't help us for the cancellation case,
  141. * since the endpoint may stop before it reaches that event data TRB.
  142. *
  143. * The radix tree maps the upper portion of the TRB DMA address to a ring
  144. * segment that has the same upper portion of DMA addresses. For example,
  145. * say I have segments of size 1KB, that are always 1KB aligned. A segment may
  146. * start at 0x10c91000 and end at 0x10c913f0. If I use the upper 10 bits, the
  147. * key to the stream ID is 0x43244. I can use the DMA address of the TRB to
  148. * pass the radix tree a key to get the right stream ID:
  149. *
  150. * 0x10c90fff >> 10 = 0x43243
  151. * 0x10c912c0 >> 10 = 0x43244
  152. * 0x10c91400 >> 10 = 0x43245
  153. *
  154. * Obviously, only those TRBs with DMA addresses that are within the segment
  155. * will make the radix tree return the stream ID for that ring.
  156. *
  157. * Caveats for the radix tree:
  158. *
  159. * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an
  160. * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be
  161. * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the
  162. * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit
  163. * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit
  164. * extended systems (where the DMA address can be bigger than 32-bits),
  165. * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that.
  166. */
  167. static int cdnsp_insert_segment_mapping(struct radix_tree_root *trb_address_map,
  168. struct cdnsp_ring *ring,
  169. struct cdnsp_segment *seg,
  170. gfp_t mem_flags)
  171. {
  172. unsigned long key;
  173. int ret;
  174. key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
  175. /* Skip any segments that were already added. */
  176. if (radix_tree_lookup(trb_address_map, key))
  177. return 0;
  178. ret = radix_tree_maybe_preload(mem_flags);
  179. if (ret)
  180. return ret;
  181. ret = radix_tree_insert(trb_address_map, key, ring);
  182. radix_tree_preload_end();
  183. return ret;
  184. }
  185. static void cdnsp_remove_segment_mapping(struct radix_tree_root *trb_address_map,
  186. struct cdnsp_segment *seg)
  187. {
  188. unsigned long key;
  189. key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
  190. if (radix_tree_lookup(trb_address_map, key))
  191. radix_tree_delete(trb_address_map, key);
  192. }
  193. static int cdnsp_update_stream_segment_mapping(struct radix_tree_root *trb_address_map,
  194. struct cdnsp_ring *ring,
  195. struct cdnsp_segment *first_seg,
  196. struct cdnsp_segment *last_seg,
  197. gfp_t mem_flags)
  198. {
  199. struct cdnsp_segment *failed_seg;
  200. struct cdnsp_segment *seg;
  201. int ret;
  202. seg = first_seg;
  203. do {
  204. ret = cdnsp_insert_segment_mapping(trb_address_map, ring, seg,
  205. mem_flags);
  206. if (ret)
  207. goto remove_streams;
  208. if (seg == last_seg)
  209. return 0;
  210. seg = seg->next;
  211. } while (seg != first_seg);
  212. return 0;
  213. remove_streams:
  214. failed_seg = seg;
  215. seg = first_seg;
  216. do {
  217. cdnsp_remove_segment_mapping(trb_address_map, seg);
  218. if (seg == failed_seg)
  219. return ret;
  220. seg = seg->next;
  221. } while (seg != first_seg);
  222. return ret;
  223. }
  224. static void cdnsp_remove_stream_mapping(struct cdnsp_ring *ring)
  225. {
  226. struct cdnsp_segment *seg;
  227. seg = ring->first_seg;
  228. do {
  229. cdnsp_remove_segment_mapping(ring->trb_address_map, seg);
  230. seg = seg->next;
  231. } while (seg != ring->first_seg);
  232. }
  233. static int cdnsp_update_stream_mapping(struct cdnsp_ring *ring)
  234. {
  235. return cdnsp_update_stream_segment_mapping(ring->trb_address_map, ring,
  236. ring->first_seg, ring->last_seg, GFP_ATOMIC);
  237. }
  238. static void cdnsp_ring_free(struct cdnsp_device *pdev, struct cdnsp_ring *ring)
  239. {
  240. if (!ring)
  241. return;
  242. trace_cdnsp_ring_free(ring);
  243. if (ring->first_seg) {
  244. if (ring->type == TYPE_STREAM)
  245. cdnsp_remove_stream_mapping(ring);
  246. cdnsp_free_segments_for_ring(pdev, ring->first_seg);
  247. }
  248. kfree(ring);
  249. }
  250. void cdnsp_initialize_ring_info(struct cdnsp_ring *ring)
  251. {
  252. ring->enqueue = ring->first_seg->trbs;
  253. ring->enq_seg = ring->first_seg;
  254. ring->dequeue = ring->enqueue;
  255. ring->deq_seg = ring->first_seg;
  256. /*
  257. * The ring is initialized to 0. The producer must write 1 to the cycle
  258. * bit to handover ownership of the TRB, so PCS = 1. The consumer must
  259. * compare CCS to the cycle bit to check ownership, so CCS = 1.
  260. *
  261. * New rings are initialized with cycle state equal to 1; if we are
  262. * handling ring expansion, set the cycle state equal to the old ring.
  263. */
  264. ring->cycle_state = 1;
  265. /*
  266. * Each segment has a link TRB, and leave an extra TRB for SW
  267. * accounting purpose
  268. */
  269. ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
  270. }
  271. /* Allocate segments and link them for a ring. */
  272. static int cdnsp_alloc_segments_for_ring(struct cdnsp_device *pdev,
  273. struct cdnsp_segment **first,
  274. struct cdnsp_segment **last,
  275. unsigned int num_segs,
  276. unsigned int cycle_state,
  277. enum cdnsp_ring_type type,
  278. unsigned int max_packet,
  279. gfp_t flags)
  280. {
  281. struct cdnsp_segment *prev;
  282. /* Allocate first segment. */
  283. prev = cdnsp_segment_alloc(pdev, cycle_state, max_packet, flags);
  284. if (!prev)
  285. return -ENOMEM;
  286. num_segs--;
  287. *first = prev;
  288. /* Allocate all other segments. */
  289. while (num_segs > 0) {
  290. struct cdnsp_segment *next;
  291. next = cdnsp_segment_alloc(pdev, cycle_state,
  292. max_packet, flags);
  293. if (!next) {
  294. cdnsp_free_segments_for_ring(pdev, *first);
  295. return -ENOMEM;
  296. }
  297. cdnsp_link_segments(pdev, prev, next, type);
  298. prev = next;
  299. num_segs--;
  300. }
  301. cdnsp_link_segments(pdev, prev, *first, type);
  302. *last = prev;
  303. return 0;
  304. }
  305. /*
  306. * Create a new ring with zero or more segments.
  307. *
  308. * Link each segment together into a ring.
  309. * Set the end flag and the cycle toggle bit on the last segment.
  310. */
  311. static struct cdnsp_ring *cdnsp_ring_alloc(struct cdnsp_device *pdev,
  312. unsigned int num_segs,
  313. enum cdnsp_ring_type type,
  314. unsigned int max_packet,
  315. gfp_t flags)
  316. {
  317. struct cdnsp_ring *ring;
  318. int ret;
  319. ring = kzalloc_obj(*(ring), flags);
  320. if (!ring)
  321. return NULL;
  322. ring->num_segs = num_segs;
  323. ring->bounce_buf_len = max_packet;
  324. INIT_LIST_HEAD(&ring->td_list);
  325. ring->type = type;
  326. if (num_segs == 0)
  327. return ring;
  328. ret = cdnsp_alloc_segments_for_ring(pdev, &ring->first_seg,
  329. &ring->last_seg, num_segs,
  330. 1, type, max_packet, flags);
  331. if (ret)
  332. goto fail;
  333. /* Only event ring does not use link TRB. */
  334. if (type != TYPE_EVENT)
  335. ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control |=
  336. cpu_to_le32(LINK_TOGGLE);
  337. cdnsp_initialize_ring_info(ring);
  338. trace_cdnsp_ring_alloc(ring);
  339. return ring;
  340. fail:
  341. kfree(ring);
  342. return NULL;
  343. }
  344. void cdnsp_free_endpoint_rings(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
  345. {
  346. cdnsp_ring_free(pdev, pep->ring);
  347. pep->ring = NULL;
  348. cdnsp_free_stream_info(pdev, pep);
  349. }
  350. /*
  351. * Expand an existing ring.
  352. * Allocate a new ring which has same segment numbers and link the two rings.
  353. */
  354. int cdnsp_ring_expansion(struct cdnsp_device *pdev,
  355. struct cdnsp_ring *ring,
  356. unsigned int num_trbs,
  357. gfp_t flags)
  358. {
  359. unsigned int num_segs_needed;
  360. struct cdnsp_segment *first;
  361. struct cdnsp_segment *last;
  362. unsigned int num_segs;
  363. int ret;
  364. num_segs_needed = (num_trbs + (TRBS_PER_SEGMENT - 1) - 1) /
  365. (TRBS_PER_SEGMENT - 1);
  366. /* Allocate number of segments we needed, or double the ring size. */
  367. num_segs = max(ring->num_segs, num_segs_needed);
  368. ret = cdnsp_alloc_segments_for_ring(pdev, &first, &last, num_segs,
  369. ring->cycle_state, ring->type,
  370. ring->bounce_buf_len, flags);
  371. if (ret)
  372. return -ENOMEM;
  373. if (ring->type == TYPE_STREAM)
  374. ret = cdnsp_update_stream_segment_mapping(ring->trb_address_map,
  375. ring, first,
  376. last, flags);
  377. if (ret) {
  378. cdnsp_free_segments_for_ring(pdev, first);
  379. return ret;
  380. }
  381. cdnsp_link_rings(pdev, ring, first, last, num_segs);
  382. trace_cdnsp_ring_expansion(ring);
  383. return 0;
  384. }
  385. static int cdnsp_init_device_ctx(struct cdnsp_device *pdev)
  386. {
  387. int size = HCC_64BYTE_CONTEXT(pdev->hcc_params) ? 2048 : 1024;
  388. pdev->out_ctx.type = CDNSP_CTX_TYPE_DEVICE;
  389. pdev->out_ctx.size = size;
  390. pdev->out_ctx.ctx_size = CTX_SIZE(pdev->hcc_params);
  391. pdev->out_ctx.bytes = dma_pool_zalloc(pdev->device_pool, GFP_ATOMIC,
  392. &pdev->out_ctx.dma);
  393. if (!pdev->out_ctx.bytes)
  394. return -ENOMEM;
  395. pdev->in_ctx.type = CDNSP_CTX_TYPE_INPUT;
  396. pdev->in_ctx.ctx_size = pdev->out_ctx.ctx_size;
  397. pdev->in_ctx.size = size + pdev->out_ctx.ctx_size;
  398. pdev->in_ctx.bytes = dma_pool_zalloc(pdev->device_pool, GFP_ATOMIC,
  399. &pdev->in_ctx.dma);
  400. if (!pdev->in_ctx.bytes) {
  401. dma_pool_free(pdev->device_pool, pdev->out_ctx.bytes,
  402. pdev->out_ctx.dma);
  403. return -ENOMEM;
  404. }
  405. return 0;
  406. }
  407. struct cdnsp_input_control_ctx
  408. *cdnsp_get_input_control_ctx(struct cdnsp_container_ctx *ctx)
  409. {
  410. if (ctx->type != CDNSP_CTX_TYPE_INPUT)
  411. return NULL;
  412. return (struct cdnsp_input_control_ctx *)ctx->bytes;
  413. }
  414. struct cdnsp_slot_ctx *cdnsp_get_slot_ctx(struct cdnsp_container_ctx *ctx)
  415. {
  416. if (ctx->type == CDNSP_CTX_TYPE_DEVICE)
  417. return (struct cdnsp_slot_ctx *)ctx->bytes;
  418. return (struct cdnsp_slot_ctx *)(ctx->bytes + ctx->ctx_size);
  419. }
  420. struct cdnsp_ep_ctx *cdnsp_get_ep_ctx(struct cdnsp_container_ctx *ctx,
  421. unsigned int ep_index)
  422. {
  423. /* Increment ep index by offset of start of ep ctx array. */
  424. ep_index++;
  425. if (ctx->type == CDNSP_CTX_TYPE_INPUT)
  426. ep_index++;
  427. return (struct cdnsp_ep_ctx *)(ctx->bytes + (ep_index * ctx->ctx_size));
  428. }
  429. static void cdnsp_free_stream_ctx(struct cdnsp_device *pdev,
  430. struct cdnsp_ep *pep)
  431. {
  432. dma_pool_free(pdev->device_pool, pep->stream_info.stream_ctx_array,
  433. pep->stream_info.ctx_array_dma);
  434. }
  435. /* The stream context array must be a power of 2. */
  436. static struct cdnsp_stream_ctx
  437. *cdnsp_alloc_stream_ctx(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
  438. {
  439. size_t size = sizeof(struct cdnsp_stream_ctx) *
  440. pep->stream_info.num_stream_ctxs;
  441. if (size > CDNSP_CTX_SIZE)
  442. return NULL;
  443. /**
  444. * Driver uses intentionally the device_pool to allocated stream
  445. * context array. Device Pool has 2048 bytes of size what gives us
  446. * 128 entries.
  447. */
  448. return dma_pool_zalloc(pdev->device_pool, GFP_DMA32 | GFP_ATOMIC,
  449. &pep->stream_info.ctx_array_dma);
  450. }
  451. struct cdnsp_ring *cdnsp_dma_to_transfer_ring(struct cdnsp_ep *pep, u64 address)
  452. {
  453. if (pep->ep_state & EP_HAS_STREAMS)
  454. return radix_tree_lookup(&pep->stream_info.trb_address_map,
  455. address >> TRB_SEGMENT_SHIFT);
  456. return pep->ring;
  457. }
  458. /*
  459. * Change an endpoint's internal structure so it supports stream IDs.
  460. * The number of requested streams includes stream 0, which cannot be used by
  461. * driver.
  462. *
  463. * The number of stream contexts in the stream context array may be bigger than
  464. * the number of streams the driver wants to use. This is because the number of
  465. * stream context array entries must be a power of two.
  466. */
  467. int cdnsp_alloc_stream_info(struct cdnsp_device *pdev,
  468. struct cdnsp_ep *pep,
  469. unsigned int num_stream_ctxs,
  470. unsigned int num_streams)
  471. {
  472. struct cdnsp_stream_info *stream_info;
  473. struct cdnsp_ring *cur_ring;
  474. u32 cur_stream;
  475. u64 addr;
  476. int ret;
  477. int mps;
  478. stream_info = &pep->stream_info;
  479. stream_info->num_streams = num_streams;
  480. stream_info->num_stream_ctxs = num_stream_ctxs;
  481. /* Initialize the array of virtual pointers to stream rings. */
  482. stream_info->stream_rings = kzalloc_objs(struct cdnsp_ring *,
  483. num_streams, GFP_ATOMIC);
  484. if (!stream_info->stream_rings)
  485. return -ENOMEM;
  486. /* Initialize the array of DMA addresses for stream rings for the HW. */
  487. stream_info->stream_ctx_array = cdnsp_alloc_stream_ctx(pdev, pep);
  488. if (!stream_info->stream_ctx_array)
  489. goto cleanup_stream_rings;
  490. memset(stream_info->stream_ctx_array, 0,
  491. sizeof(struct cdnsp_stream_ctx) * num_stream_ctxs);
  492. INIT_RADIX_TREE(&stream_info->trb_address_map, GFP_ATOMIC);
  493. mps = usb_endpoint_maxp(pep->endpoint.desc);
  494. /*
  495. * Allocate rings for all the streams that the driver will use,
  496. * and add their segment DMA addresses to the radix tree.
  497. * Stream 0 is reserved.
  498. */
  499. for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
  500. cur_ring = cdnsp_ring_alloc(pdev, 2, TYPE_STREAM, mps,
  501. GFP_ATOMIC);
  502. stream_info->stream_rings[cur_stream] = cur_ring;
  503. if (!cur_ring)
  504. goto cleanup_rings;
  505. cur_ring->stream_id = cur_stream;
  506. cur_ring->trb_address_map = &stream_info->trb_address_map;
  507. /* Set deq ptr, cycle bit, and stream context type. */
  508. addr = cur_ring->first_seg->dma | SCT_FOR_CTX(SCT_PRI_TR) |
  509. cur_ring->cycle_state;
  510. stream_info->stream_ctx_array[cur_stream].stream_ring =
  511. cpu_to_le64(addr);
  512. trace_cdnsp_set_stream_ring(cur_ring);
  513. ret = cdnsp_update_stream_mapping(cur_ring);
  514. if (ret)
  515. goto cleanup_rings;
  516. }
  517. return 0;
  518. cleanup_rings:
  519. for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
  520. cur_ring = stream_info->stream_rings[cur_stream];
  521. if (cur_ring) {
  522. cdnsp_ring_free(pdev, cur_ring);
  523. stream_info->stream_rings[cur_stream] = NULL;
  524. }
  525. }
  526. cleanup_stream_rings:
  527. kfree(pep->stream_info.stream_rings);
  528. return -ENOMEM;
  529. }
  530. /* Frees all stream contexts associated with the endpoint. */
  531. static void cdnsp_free_stream_info(struct cdnsp_device *pdev,
  532. struct cdnsp_ep *pep)
  533. {
  534. struct cdnsp_stream_info *stream_info = &pep->stream_info;
  535. struct cdnsp_ring *cur_ring;
  536. int cur_stream;
  537. if (!(pep->ep_state & EP_HAS_STREAMS))
  538. return;
  539. for (cur_stream = 1; cur_stream < stream_info->num_streams;
  540. cur_stream++) {
  541. cur_ring = stream_info->stream_rings[cur_stream];
  542. if (cur_ring) {
  543. cdnsp_ring_free(pdev, cur_ring);
  544. stream_info->stream_rings[cur_stream] = NULL;
  545. }
  546. }
  547. if (stream_info->stream_ctx_array)
  548. cdnsp_free_stream_ctx(pdev, pep);
  549. kfree(stream_info->stream_rings);
  550. pep->ep_state &= ~EP_HAS_STREAMS;
  551. }
  552. /* All the cdnsp_tds in the ring's TD list should be freed at this point.*/
  553. static void cdnsp_free_priv_device(struct cdnsp_device *pdev)
  554. {
  555. pdev->dcbaa->dev_context_ptrs[1] = 0;
  556. cdnsp_free_endpoint_rings(pdev, &pdev->eps[0]);
  557. if (pdev->in_ctx.bytes)
  558. dma_pool_free(pdev->device_pool, pdev->in_ctx.bytes,
  559. pdev->in_ctx.dma);
  560. if (pdev->out_ctx.bytes)
  561. dma_pool_free(pdev->device_pool, pdev->out_ctx.bytes,
  562. pdev->out_ctx.dma);
  563. pdev->in_ctx.bytes = NULL;
  564. pdev->out_ctx.bytes = NULL;
  565. }
  566. static int cdnsp_alloc_priv_device(struct cdnsp_device *pdev)
  567. {
  568. int ret;
  569. ret = cdnsp_init_device_ctx(pdev);
  570. if (ret)
  571. return ret;
  572. /* Allocate endpoint 0 ring. */
  573. pdev->eps[0].ring = cdnsp_ring_alloc(pdev, 2, TYPE_CTRL, 0, GFP_ATOMIC);
  574. if (!pdev->eps[0].ring)
  575. goto fail;
  576. /* Point to output device context in dcbaa. */
  577. pdev->dcbaa->dev_context_ptrs[1] = cpu_to_le64(pdev->out_ctx.dma);
  578. pdev->cmd.in_ctx = &pdev->in_ctx;
  579. trace_cdnsp_alloc_priv_device(pdev);
  580. return 0;
  581. fail:
  582. dma_pool_free(pdev->device_pool, pdev->out_ctx.bytes,
  583. pdev->out_ctx.dma);
  584. dma_pool_free(pdev->device_pool, pdev->in_ctx.bytes,
  585. pdev->in_ctx.dma);
  586. return ret;
  587. }
  588. void cdnsp_copy_ep0_dequeue_into_input_ctx(struct cdnsp_device *pdev)
  589. {
  590. struct cdnsp_ep_ctx *ep0_ctx = pdev->eps[0].in_ctx;
  591. struct cdnsp_ring *ep_ring = pdev->eps[0].ring;
  592. dma_addr_t dma;
  593. dma = cdnsp_trb_virt_to_dma(ep_ring->enq_seg, ep_ring->enqueue);
  594. ep0_ctx->deq = cpu_to_le64(dma | ep_ring->cycle_state);
  595. }
  596. /* Setup an controller private device for a Set Address command. */
  597. int cdnsp_setup_addressable_priv_dev(struct cdnsp_device *pdev)
  598. {
  599. struct cdnsp_slot_ctx *slot_ctx;
  600. struct cdnsp_ep_ctx *ep0_ctx;
  601. u32 max_packets, port;
  602. ep0_ctx = cdnsp_get_ep_ctx(&pdev->in_ctx, 0);
  603. slot_ctx = cdnsp_get_slot_ctx(&pdev->in_ctx);
  604. /* Only the control endpoint is valid - one endpoint context. */
  605. slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
  606. switch (pdev->gadget.speed) {
  607. case USB_SPEED_SUPER_PLUS:
  608. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SSP);
  609. max_packets = MAX_PACKET(512);
  610. break;
  611. case USB_SPEED_SUPER:
  612. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
  613. max_packets = MAX_PACKET(512);
  614. break;
  615. case USB_SPEED_HIGH:
  616. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
  617. max_packets = MAX_PACKET(64);
  618. break;
  619. case USB_SPEED_FULL:
  620. slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
  621. max_packets = MAX_PACKET(64);
  622. break;
  623. default:
  624. /* Speed was not set , this shouldn't happen. */
  625. return -EINVAL;
  626. }
  627. port = DEV_PORT(pdev->active_port->port_num);
  628. slot_ctx->dev_port |= cpu_to_le32(port);
  629. slot_ctx->dev_state = cpu_to_le32((pdev->device_address &
  630. DEV_ADDR_MASK));
  631. ep0_ctx->tx_info = cpu_to_le32(EP_AVG_TRB_LENGTH(0x8));
  632. ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP));
  633. ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3) |
  634. max_packets);
  635. ep0_ctx->deq = cpu_to_le64(pdev->eps[0].ring->first_seg->dma |
  636. pdev->eps[0].ring->cycle_state);
  637. trace_cdnsp_setup_addressable_priv_device(pdev);
  638. return 0;
  639. }
  640. /*
  641. * Convert interval expressed as 2^(bInterval - 1) == interval into
  642. * straight exponent value 2^n == interval.
  643. */
  644. static unsigned int cdnsp_parse_exponent_interval(struct usb_gadget *g,
  645. struct cdnsp_ep *pep)
  646. {
  647. unsigned int interval;
  648. interval = clamp_val(pep->endpoint.desc->bInterval, 1, 16) - 1;
  649. if (interval != pep->endpoint.desc->bInterval - 1)
  650. dev_warn(&g->dev, "ep %s - rounding interval to %d %sframes\n",
  651. pep->name, 1 << interval,
  652. g->speed == USB_SPEED_FULL ? "" : "micro");
  653. /*
  654. * Full speed isoc endpoints specify interval in frames,
  655. * not microframes. We are using microframes everywhere,
  656. * so adjust accordingly.
  657. */
  658. if (g->speed == USB_SPEED_FULL)
  659. interval += 3; /* 1 frame = 2^3 uframes */
  660. /* Controller handles only up to 512ms (2^12). */
  661. if (interval > 12)
  662. interval = 12;
  663. return interval;
  664. }
  665. /*
  666. * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
  667. * microframes, rounded down to nearest power of 2.
  668. */
  669. static unsigned int cdnsp_microframes_to_exponent(struct usb_gadget *g,
  670. struct cdnsp_ep *pep,
  671. unsigned int desc_interval,
  672. unsigned int min_exponent,
  673. unsigned int max_exponent)
  674. {
  675. unsigned int interval;
  676. interval = fls(desc_interval) - 1;
  677. return clamp_val(interval, min_exponent, max_exponent);
  678. }
  679. /*
  680. * Return the polling interval.
  681. *
  682. * The polling interval is expressed in "microframes". If controllers's Interval
  683. * field is set to N, it will service the endpoint every 2^(Interval)*125us.
  684. */
  685. static unsigned int cdnsp_get_endpoint_interval(struct usb_gadget *g,
  686. struct cdnsp_ep *pep)
  687. {
  688. unsigned int interval = 0;
  689. switch (g->speed) {
  690. case USB_SPEED_HIGH:
  691. case USB_SPEED_SUPER_PLUS:
  692. case USB_SPEED_SUPER:
  693. if (usb_endpoint_xfer_int(pep->endpoint.desc) ||
  694. usb_endpoint_xfer_isoc(pep->endpoint.desc))
  695. interval = cdnsp_parse_exponent_interval(g, pep);
  696. break;
  697. case USB_SPEED_FULL:
  698. if (usb_endpoint_xfer_isoc(pep->endpoint.desc)) {
  699. interval = cdnsp_parse_exponent_interval(g, pep);
  700. } else if (usb_endpoint_xfer_int(pep->endpoint.desc)) {
  701. interval = pep->endpoint.desc->bInterval << 3;
  702. interval = cdnsp_microframes_to_exponent(g, pep,
  703. interval,
  704. 3, 10);
  705. }
  706. break;
  707. default:
  708. WARN_ON(1);
  709. }
  710. return interval;
  711. }
  712. /*
  713. * The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
  714. * High speed endpoint descriptors can define "the number of additional
  715. * transaction opportunities per microframe", but that goes in the Max Burst
  716. * endpoint context field.
  717. */
  718. static u32 cdnsp_get_endpoint_mult(struct usb_gadget *g, struct cdnsp_ep *pep)
  719. {
  720. if (g->speed < USB_SPEED_SUPER ||
  721. !usb_endpoint_xfer_isoc(pep->endpoint.desc))
  722. return 0;
  723. return pep->endpoint.comp_desc->bmAttributes;
  724. }
  725. static u32 cdnsp_get_endpoint_max_burst(struct usb_gadget *g,
  726. struct cdnsp_ep *pep)
  727. {
  728. /* Super speed and Plus have max burst in ep companion desc */
  729. if (g->speed >= USB_SPEED_SUPER)
  730. return pep->endpoint.comp_desc->bMaxBurst;
  731. if (g->speed == USB_SPEED_HIGH &&
  732. (usb_endpoint_xfer_isoc(pep->endpoint.desc) ||
  733. usb_endpoint_xfer_int(pep->endpoint.desc)))
  734. return usb_endpoint_maxp_mult(pep->endpoint.desc) - 1;
  735. return 0;
  736. }
  737. static u32 cdnsp_get_endpoint_type(const struct usb_endpoint_descriptor *desc)
  738. {
  739. int in;
  740. in = usb_endpoint_dir_in(desc);
  741. switch (usb_endpoint_type(desc)) {
  742. case USB_ENDPOINT_XFER_CONTROL:
  743. return CTRL_EP;
  744. case USB_ENDPOINT_XFER_BULK:
  745. return in ? BULK_IN_EP : BULK_OUT_EP;
  746. case USB_ENDPOINT_XFER_ISOC:
  747. return in ? ISOC_IN_EP : ISOC_OUT_EP;
  748. case USB_ENDPOINT_XFER_INT:
  749. return in ? INT_IN_EP : INT_OUT_EP;
  750. }
  751. return 0;
  752. }
  753. /*
  754. * Return the maximum endpoint service interval time (ESIT) payload.
  755. * Basically, this is the maxpacket size, multiplied by the burst size
  756. * and mult size.
  757. */
  758. static u32 cdnsp_get_max_esit_payload(struct usb_gadget *g,
  759. struct cdnsp_ep *pep)
  760. {
  761. int max_packet;
  762. int max_burst;
  763. /* Only applies for interrupt or isochronous endpoints*/
  764. if (usb_endpoint_xfer_control(pep->endpoint.desc) ||
  765. usb_endpoint_xfer_bulk(pep->endpoint.desc))
  766. return 0;
  767. /* SuperSpeedPlus Isoc ep sending over 48k per EIST. */
  768. if (g->speed >= USB_SPEED_SUPER_PLUS &&
  769. USB_SS_SSP_ISOC_COMP(pep->endpoint.desc->bmAttributes))
  770. return le16_to_cpu(pep->endpoint.comp_desc->wBytesPerInterval);
  771. /* SuperSpeed or SuperSpeedPlus Isoc ep with less than 48k per esit */
  772. else if (g->speed >= USB_SPEED_SUPER)
  773. return le16_to_cpu(pep->endpoint.comp_desc->wBytesPerInterval);
  774. max_packet = usb_endpoint_maxp(pep->endpoint.desc);
  775. max_burst = usb_endpoint_maxp_mult(pep->endpoint.desc);
  776. /* A 0 in max burst means 1 transfer per ESIT */
  777. return max_packet * max_burst;
  778. }
  779. int cdnsp_endpoint_init(struct cdnsp_device *pdev,
  780. struct cdnsp_ep *pep,
  781. gfp_t mem_flags)
  782. {
  783. enum cdnsp_ring_type ring_type;
  784. struct cdnsp_ep_ctx *ep_ctx;
  785. unsigned int err_count = 0;
  786. unsigned int avg_trb_len;
  787. unsigned int max_packet;
  788. unsigned int max_burst;
  789. unsigned int interval;
  790. u32 max_esit_payload;
  791. unsigned int mult;
  792. u32 endpoint_type;
  793. int ret;
  794. ep_ctx = pep->in_ctx;
  795. endpoint_type = cdnsp_get_endpoint_type(pep->endpoint.desc);
  796. if (!endpoint_type)
  797. return -EINVAL;
  798. ring_type = usb_endpoint_type(pep->endpoint.desc);
  799. /*
  800. * Get values to fill the endpoint context, mostly from ep descriptor.
  801. * The average TRB buffer length for bulk endpoints is unclear as we
  802. * have no clue on scatter gather list entry size. For Isoc and Int,
  803. * set it to max available.
  804. */
  805. max_esit_payload = cdnsp_get_max_esit_payload(&pdev->gadget, pep);
  806. interval = cdnsp_get_endpoint_interval(&pdev->gadget, pep);
  807. mult = cdnsp_get_endpoint_mult(&pdev->gadget, pep);
  808. max_packet = usb_endpoint_maxp(pep->endpoint.desc);
  809. max_burst = cdnsp_get_endpoint_max_burst(&pdev->gadget, pep);
  810. avg_trb_len = max_esit_payload;
  811. /* Allow 3 retries for everything but isoc, set CErr = 3. */
  812. if (!usb_endpoint_xfer_isoc(pep->endpoint.desc))
  813. err_count = 3;
  814. if (usb_endpoint_xfer_bulk(pep->endpoint.desc) &&
  815. pdev->gadget.speed == USB_SPEED_HIGH)
  816. max_packet = 512;
  817. /* Controller spec indicates that ctrl ep avg TRB Length should be 8. */
  818. if (usb_endpoint_xfer_control(pep->endpoint.desc))
  819. avg_trb_len = 8;
  820. /* Set up the endpoint ring. */
  821. pep->ring = cdnsp_ring_alloc(pdev, 2, ring_type, max_packet, mem_flags);
  822. if (!pep->ring)
  823. return -ENOMEM;
  824. pep->skip = false;
  825. /* Fill the endpoint context */
  826. ep_ctx->ep_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
  827. EP_INTERVAL(interval) | EP_MULT(mult));
  828. ep_ctx->ep_info2 = cpu_to_le32(EP_TYPE(endpoint_type) |
  829. MAX_PACKET(max_packet) | MAX_BURST(max_burst) |
  830. ERROR_COUNT(err_count));
  831. ep_ctx->deq = cpu_to_le64(pep->ring->first_seg->dma |
  832. pep->ring->cycle_state);
  833. ep_ctx->tx_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
  834. EP_AVG_TRB_LENGTH(avg_trb_len));
  835. if (usb_endpoint_xfer_bulk(pep->endpoint.desc) &&
  836. pdev->gadget.speed > USB_SPEED_HIGH) {
  837. ret = cdnsp_alloc_streams(pdev, pep);
  838. if (ret < 0)
  839. return ret;
  840. }
  841. return 0;
  842. }
  843. void cdnsp_endpoint_zero(struct cdnsp_device *pdev, struct cdnsp_ep *pep)
  844. {
  845. pep->in_ctx->ep_info = 0;
  846. pep->in_ctx->ep_info2 = 0;
  847. pep->in_ctx->deq = 0;
  848. pep->in_ctx->tx_info = 0;
  849. }
  850. static int cdnsp_alloc_erst(struct cdnsp_device *pdev,
  851. struct cdnsp_ring *evt_ring,
  852. struct cdnsp_erst *erst)
  853. {
  854. struct cdnsp_erst_entry *entry;
  855. struct cdnsp_segment *seg;
  856. unsigned int val;
  857. size_t size;
  858. size = sizeof(struct cdnsp_erst_entry) * evt_ring->num_segs;
  859. erst->entries = dma_alloc_coherent(pdev->dev, size,
  860. &erst->erst_dma_addr, GFP_KERNEL);
  861. if (!erst->entries)
  862. return -ENOMEM;
  863. erst->num_entries = evt_ring->num_segs;
  864. seg = evt_ring->first_seg;
  865. for (val = 0; val < evt_ring->num_segs; val++) {
  866. entry = &erst->entries[val];
  867. entry->seg_addr = cpu_to_le64(seg->dma);
  868. entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
  869. entry->rsvd = 0;
  870. seg = seg->next;
  871. }
  872. return 0;
  873. }
  874. static void cdnsp_free_erst(struct cdnsp_device *pdev, struct cdnsp_erst *erst)
  875. {
  876. size_t size = sizeof(struct cdnsp_erst_entry) * (erst->num_entries);
  877. struct device *dev = pdev->dev;
  878. if (erst->entries)
  879. dma_free_coherent(dev, size, erst->entries,
  880. erst->erst_dma_addr);
  881. erst->entries = NULL;
  882. }
  883. void cdnsp_mem_cleanup(struct cdnsp_device *pdev)
  884. {
  885. struct device *dev = pdev->dev;
  886. cdnsp_free_priv_device(pdev);
  887. cdnsp_free_erst(pdev, &pdev->erst);
  888. if (pdev->event_ring)
  889. cdnsp_ring_free(pdev, pdev->event_ring);
  890. pdev->event_ring = NULL;
  891. if (pdev->cmd_ring)
  892. cdnsp_ring_free(pdev, pdev->cmd_ring);
  893. pdev->cmd_ring = NULL;
  894. dma_pool_destroy(pdev->segment_pool);
  895. pdev->segment_pool = NULL;
  896. dma_pool_destroy(pdev->device_pool);
  897. pdev->device_pool = NULL;
  898. dma_free_coherent(dev, sizeof(*pdev->dcbaa),
  899. pdev->dcbaa, pdev->dcbaa->dma);
  900. pdev->dcbaa = NULL;
  901. pdev->usb2_port.exist = 0;
  902. pdev->usb3_port.exist = 0;
  903. pdev->usb2_port.port_num = 0;
  904. pdev->usb3_port.port_num = 0;
  905. pdev->active_port = NULL;
  906. }
  907. static void cdnsp_set_event_deq(struct cdnsp_device *pdev)
  908. {
  909. dma_addr_t deq;
  910. u64 temp;
  911. deq = cdnsp_trb_virt_to_dma(pdev->event_ring->deq_seg,
  912. pdev->event_ring->dequeue);
  913. /* Update controller event ring dequeue pointer */
  914. temp = cdnsp_read_64(&pdev->ir_set->erst_dequeue);
  915. temp &= ERST_PTR_MASK;
  916. /*
  917. * Don't clear the EHB bit (which is RW1C) because
  918. * there might be more events to service.
  919. */
  920. temp &= ~ERST_EHB;
  921. cdnsp_write_64(((u64)deq & (u64)~ERST_PTR_MASK) | temp,
  922. &pdev->ir_set->erst_dequeue);
  923. }
  924. static void cdnsp_add_in_port(struct cdnsp_device *pdev,
  925. struct cdnsp_port *port,
  926. __le32 __iomem *addr)
  927. {
  928. u32 temp, port_offset, port_count;
  929. temp = readl(addr);
  930. port->maj_rev = CDNSP_EXT_PORT_MAJOR(temp);
  931. port->min_rev = CDNSP_EXT_PORT_MINOR(temp);
  932. /* Port offset and count in the third dword.*/
  933. temp = readl(addr + 2);
  934. port_offset = CDNSP_EXT_PORT_OFF(temp);
  935. port_count = CDNSP_EXT_PORT_COUNT(temp);
  936. trace_cdnsp_port_info(addr, port_offset, port_count, port->maj_rev);
  937. port->port_num = port_offset;
  938. port->exist = 1;
  939. }
  940. /*
  941. * Scan the Extended Capabilities for the "Supported Protocol Capabilities" that
  942. * specify what speeds each port is supposed to be.
  943. */
  944. static int cdnsp_setup_port_arrays(struct cdnsp_device *pdev)
  945. {
  946. void __iomem *base;
  947. u32 offset;
  948. int i;
  949. base = &pdev->cap_regs->hc_capbase;
  950. offset = cdnsp_find_next_ext_cap(base, 0,
  951. EXT_CAP_CFG_DEV_20PORT_CAP_ID);
  952. pdev->port20_regs = base + offset;
  953. offset = cdnsp_find_next_ext_cap(base, 0, D_XEC_CFG_3XPORT_CAP);
  954. pdev->port3x_regs = base + offset;
  955. offset = 0;
  956. base = &pdev->cap_regs->hc_capbase;
  957. /* Driver expects max 2 extended protocol capability. */
  958. for (i = 0; i < 2; i++) {
  959. u32 temp;
  960. offset = cdnsp_find_next_ext_cap(base, offset,
  961. EXT_CAPS_PROTOCOL);
  962. temp = readl(base + offset);
  963. if (CDNSP_EXT_PORT_MAJOR(temp) == 0x03 &&
  964. !pdev->usb3_port.port_num)
  965. cdnsp_add_in_port(pdev, &pdev->usb3_port,
  966. base + offset);
  967. if (CDNSP_EXT_PORT_MAJOR(temp) == 0x02 &&
  968. !pdev->usb2_port.port_num)
  969. cdnsp_add_in_port(pdev, &pdev->usb2_port,
  970. base + offset);
  971. }
  972. if (!pdev->usb2_port.exist || !pdev->usb3_port.exist) {
  973. dev_err(pdev->dev, "Error: Only one port detected\n");
  974. return -ENODEV;
  975. }
  976. trace_cdnsp_init("Found USB 2.0 ports and USB 3.0 ports.");
  977. pdev->usb2_port.regs = (struct cdnsp_port_regs __iomem *)
  978. (&pdev->op_regs->port_reg_base + NUM_PORT_REGS *
  979. (pdev->usb2_port.port_num - 1));
  980. pdev->usb3_port.regs = (struct cdnsp_port_regs __iomem *)
  981. (&pdev->op_regs->port_reg_base + NUM_PORT_REGS *
  982. (pdev->usb3_port.port_num - 1));
  983. return 0;
  984. }
  985. /*
  986. * Initialize memory for CDNSP (one-time init).
  987. *
  988. * Program the PAGESIZE register, initialize the device context array, create
  989. * device contexts, set up a command ring segment, create event
  990. * ring (one for now).
  991. */
  992. int cdnsp_mem_init(struct cdnsp_device *pdev)
  993. {
  994. struct device *dev = pdev->dev;
  995. int ret = -ENOMEM;
  996. unsigned int val;
  997. dma_addr_t dma;
  998. u32 page_size;
  999. u64 val_64;
  1000. /*
  1001. * Use 4K pages, since that's common and the minimum the
  1002. * controller supports
  1003. */
  1004. page_size = 1 << 12;
  1005. val = readl(&pdev->op_regs->config_reg);
  1006. val |= ((val & ~MAX_DEVS) | CDNSP_DEV_MAX_SLOTS) | CONFIG_U3E;
  1007. writel(val, &pdev->op_regs->config_reg);
  1008. /*
  1009. * Doorbell array must be physically contiguous
  1010. * and 64-byte (cache line) aligned.
  1011. */
  1012. pdev->dcbaa = dma_alloc_coherent(dev, sizeof(*pdev->dcbaa),
  1013. &dma, GFP_KERNEL);
  1014. if (!pdev->dcbaa)
  1015. return -ENOMEM;
  1016. pdev->dcbaa->dma = dma;
  1017. cdnsp_write_64(dma, &pdev->op_regs->dcbaa_ptr);
  1018. /*
  1019. * Initialize the ring segment pool. The ring must be a contiguous
  1020. * structure comprised of TRBs. The TRBs must be 16 byte aligned,
  1021. * however, the command ring segment needs 64-byte aligned segments
  1022. * and our use of dma addresses in the trb_address_map radix tree needs
  1023. * TRB_SEGMENT_SIZE alignment, so driver pick the greater alignment
  1024. * need.
  1025. */
  1026. pdev->segment_pool = dma_pool_create("CDNSP ring segments", dev,
  1027. TRB_SEGMENT_SIZE, TRB_SEGMENT_SIZE,
  1028. page_size);
  1029. if (!pdev->segment_pool)
  1030. goto release_dcbaa;
  1031. pdev->device_pool = dma_pool_create("CDNSP input/output contexts", dev,
  1032. CDNSP_CTX_SIZE, 64, page_size);
  1033. if (!pdev->device_pool)
  1034. goto destroy_segment_pool;
  1035. /* Set up the command ring to have one segments for now. */
  1036. pdev->cmd_ring = cdnsp_ring_alloc(pdev, 1, TYPE_COMMAND, 0, GFP_KERNEL);
  1037. if (!pdev->cmd_ring)
  1038. goto destroy_device_pool;
  1039. /* Set the address in the Command Ring Control register */
  1040. val_64 = cdnsp_read_64(&pdev->op_regs->cmd_ring);
  1041. val_64 = (val_64 & (u64)CMD_RING_RSVD_BITS) |
  1042. (pdev->cmd_ring->first_seg->dma & (u64)~CMD_RING_RSVD_BITS) |
  1043. pdev->cmd_ring->cycle_state;
  1044. cdnsp_write_64(val_64, &pdev->op_regs->cmd_ring);
  1045. val = readl(&pdev->cap_regs->db_off);
  1046. val &= DBOFF_MASK;
  1047. pdev->dba = (void __iomem *)pdev->cap_regs + val;
  1048. /* Set ir_set to interrupt register set 0 */
  1049. pdev->ir_set = &pdev->run_regs->ir_set[0];
  1050. /*
  1051. * Event ring setup: Allocate a normal ring, but also setup
  1052. * the event ring segment table (ERST).
  1053. */
  1054. pdev->event_ring = cdnsp_ring_alloc(pdev, ERST_NUM_SEGS, TYPE_EVENT,
  1055. 0, GFP_KERNEL);
  1056. if (!pdev->event_ring)
  1057. goto free_cmd_ring;
  1058. ret = cdnsp_alloc_erst(pdev, pdev->event_ring, &pdev->erst);
  1059. if (ret)
  1060. goto free_event_ring;
  1061. /* Set ERST count with the number of entries in the segment table. */
  1062. val = readl(&pdev->ir_set->erst_size);
  1063. val &= ERST_SIZE_MASK;
  1064. val |= ERST_NUM_SEGS;
  1065. writel(val, &pdev->ir_set->erst_size);
  1066. /* Set the segment table base address. */
  1067. val_64 = cdnsp_read_64(&pdev->ir_set->erst_base);
  1068. val_64 &= ERST_PTR_MASK;
  1069. val_64 |= (pdev->erst.erst_dma_addr & (u64)~ERST_PTR_MASK);
  1070. cdnsp_write_64(val_64, &pdev->ir_set->erst_base);
  1071. /* Set the event ring dequeue address. */
  1072. cdnsp_set_event_deq(pdev);
  1073. ret = cdnsp_setup_port_arrays(pdev);
  1074. if (ret)
  1075. goto free_erst;
  1076. ret = cdnsp_alloc_priv_device(pdev);
  1077. if (ret) {
  1078. dev_err(pdev->dev,
  1079. "Could not allocate cdnsp_device data structures\n");
  1080. goto free_erst;
  1081. }
  1082. return 0;
  1083. free_erst:
  1084. cdnsp_free_erst(pdev, &pdev->erst);
  1085. free_event_ring:
  1086. cdnsp_ring_free(pdev, pdev->event_ring);
  1087. free_cmd_ring:
  1088. cdnsp_ring_free(pdev, pdev->cmd_ring);
  1089. destroy_device_pool:
  1090. dma_pool_destroy(pdev->device_pool);
  1091. destroy_segment_pool:
  1092. dma_pool_destroy(pdev->segment_pool);
  1093. release_dcbaa:
  1094. dma_free_coherent(dev, sizeof(*pdev->dcbaa), pdev->dcbaa,
  1095. pdev->dcbaa->dma);
  1096. cdnsp_reset(pdev);
  1097. return ret;
  1098. }