ubwcp.c 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/dma-buf.h>
  8. #include <linux/slab.h>
  9. #include <linux/cdev.h>
  10. #include <linux/hashtable.h>
  11. #include <linux/scatterlist.h>
  12. #include <linux/types.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/of.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/of_address.h>
  17. #include <linux/genalloc.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/numa.h>
  21. #include <linux/memory_hotplug.h>
  22. #include <asm/page.h>
  23. #include <linux/delay.h>
  24. #include <linux/ubwcp_dma_heap.h>
  25. #include <linux/debugfs.h>
  26. MODULE_IMPORT_NS(DMA_BUF);
  27. #include "ubwcp.h"
  28. #include "ubwcp_hw.h"
  29. #include "include/uapi/ubwcp_ioctl.h"
  30. #define UBWCP_NUM_DEVICES 1
  31. #define UBWCP_DEVICE_NAME "ubwcp"
  32. #define UBWCP_BUFFER_DESC_OFFSET 64
  33. #define UBWCP_BUFFER_DESC_COUNT 256
  34. #define CACHE_ADDR(x) ((x) >> 6)
  35. #define PAGE_ADDR(x) ((x) >> 12)
  36. #define UBWCP_ALIGN(_x, _y) ((((_x) + (_y) - 1)/(_y))*(_y))
  37. //#define DBG(fmt, args...)
  38. //#define DBG_BUF_ATTR(fmt, args...)
  39. #define DBG_BUF_ATTR(fmt, args...) do { if (ubwcp_debug_trace_enable) \
  40. pr_err("ubwcp: %s(): " fmt "\n", __func__, ##args); \
  41. } while (0)
  42. #define DBG(fmt, args...) do { if (ubwcp_debug_trace_enable) \
  43. pr_err("ubwcp: %s(): " fmt "\n", __func__, ##args); \
  44. } while (0)
  45. #define ERR(fmt, args...) pr_err("ubwcp: %s(): ~~~ERROR~~~: " fmt "\n", __func__, ##args)
  46. #define FENTRY() DBG("ubwcp: %s()", __func__)
  47. #define META_DATA_PITCH_ALIGN 64
  48. #define META_DATA_HEIGHT_ALIGN 16
  49. #define META_DATA_SIZE_ALIGN 4096
  50. #define PIXEL_DATA_SIZE_ALIGN 4096
  51. struct ubwcp_desc {
  52. int idx;
  53. void *ptr;
  54. };
  55. /* TBD: confirm size of width/height */
  56. struct ubwcp_dimension {
  57. u16 width;
  58. u16 height;
  59. };
  60. struct ubwcp_plane_info {
  61. u16 pixel_bytes;
  62. u16 per_pixel;
  63. struct ubwcp_dimension tilesize_p; /* pixels */
  64. struct ubwcp_dimension macrotilesize_p; /* pixels */
  65. };
  66. struct ubwcp_image_format_info {
  67. u16 planes;
  68. struct ubwcp_plane_info p_info[2];
  69. };
  70. enum ubwcp_std_image_format {
  71. RGBA = 0,
  72. NV12 = 1,
  73. NV124R = 2,
  74. P010 = 3,
  75. TP10 = 4,
  76. P016 = 5,
  77. INFO_FORMAT_LIST_SIZE,
  78. STD_IMAGE_FORMAT_INVALID = 0xFF
  79. };
  80. struct ubwcp_driver {
  81. /* cdev related */
  82. dev_t devt;
  83. struct class *dev_class; //sysfs dev class
  84. struct device *dev_sys; //sysfs dev
  85. struct cdev cdev; //char dev
  86. /* debugfs */
  87. struct dentry *debugfs_root;
  88. /* ubwcp devices */
  89. struct device *dev; //ubwcp device
  90. struct device *dev_desc_cb; //smmu dev for descriptors
  91. struct device *dev_buf_cb; //smmu dev for ubwcp buffers
  92. void __iomem *base; //ubwcp base address
  93. struct regulator *vdd;
  94. /* interrupts */
  95. int irq_range_ck_rd;
  96. int irq_range_ck_wr;
  97. int irq_encode;
  98. int irq_decode;
  99. /* ula address pool */
  100. u64 ula_pool_base;
  101. u64 ula_pool_size;
  102. struct gen_pool *ula_pool;
  103. configure_mmap mmap_config_fptr;
  104. /* HW version */
  105. u32 hw_ver_major;
  106. u32 hw_ver_minor;
  107. /* keep track of all buffers. hash table index'ed using dma_buf ptr.
  108. * 2**8 = 256 hash values
  109. */
  110. DECLARE_HASHTABLE(buf_table, 8);
  111. /* buffer descriptor */
  112. void *buffer_desc_base; /* CPU address */
  113. dma_addr_t buffer_desc_dma_handle; /* dma address */
  114. size_t buffer_desc_size;
  115. struct ubwcp_desc desc_list[UBWCP_BUFFER_DESC_COUNT];
  116. struct ubwcp_image_format_info format_info[INFO_FORMAT_LIST_SIZE];
  117. struct mutex desc_lock; /* allocate/free descriptors */
  118. struct mutex buf_table_lock; /* add/remove dma_buf into list of managed bufffers */
  119. struct mutex ula_lock; /* allocate/free ula */
  120. struct mutex ubwcp_flush_lock; /* ubwcp flush */
  121. struct mutex hw_range_ck_lock; /* range ck */
  122. };
  123. struct ubwcp_buf {
  124. struct hlist_node hnode;
  125. struct ubwcp_driver *ubwcp;
  126. struct ubwcp_buffer_attrs buf_attr;
  127. bool perm;
  128. struct ubwcp_desc *desc;
  129. bool buf_attr_set;
  130. bool locked;
  131. enum dma_data_direction lock_dir;
  132. int lock_count;
  133. /* dma_buf info */
  134. struct dma_buf *dma_buf;
  135. struct dma_buf_attachment *attachment;
  136. struct sg_table *sgt;
  137. /* ula info */
  138. phys_addr_t ula_pa;
  139. size_t ula_size;
  140. /* meta metadata */
  141. struct ubwcp_hw_meta_metadata mmdata;
  142. struct mutex lock;
  143. };
  144. static struct ubwcp_driver *me;
  145. static int error_print_count;
  146. u32 ubwcp_debug_trace_enable;
  147. static struct ubwcp_driver *ubwcp_get_driver(void)
  148. {
  149. if (!me)
  150. WARN(1, "ubwcp: driver ptr requested but driver not initialized");
  151. return me;
  152. }
  153. static void image_format_init(struct ubwcp_driver *ubwcp)
  154. { /* planes, bytes/p, Tp , MTp */
  155. ubwcp->format_info[RGBA] = (struct ubwcp_image_format_info)
  156. {1, {{4, 1, {16, 4}, {64, 16}}}};
  157. ubwcp->format_info[NV12] = (struct ubwcp_image_format_info)
  158. {2, {{1, 1, {32, 8}, {128, 32}},
  159. {2, 1, {16, 8}, { 64, 32}}}};
  160. ubwcp->format_info[NV124R] = (struct ubwcp_image_format_info)
  161. {2, {{1, 1, {64, 4}, {256, 16}},
  162. {2, 1, {32, 4}, {128, 16}}}};
  163. ubwcp->format_info[P010] = (struct ubwcp_image_format_info)
  164. {2, {{2, 1, {32, 4}, {128, 16}},
  165. {4, 1, {16, 4}, { 64, 16}}}};
  166. ubwcp->format_info[TP10] = (struct ubwcp_image_format_info)
  167. {2, {{4, 3, {48, 4}, {192, 16}},
  168. {8, 3, {24, 4}, { 96, 16}}}};
  169. ubwcp->format_info[P016] = (struct ubwcp_image_format_info)
  170. {2, {{2, 1, {32, 4}, {128, 16}},
  171. {4, 1, {16, 4}, { 64, 16}}}};
  172. }
  173. static void ubwcp_buf_desc_list_init(struct ubwcp_driver *ubwcp)
  174. {
  175. int idx;
  176. struct ubwcp_desc *desc_list = ubwcp->desc_list;
  177. for (idx = 0; idx < UBWCP_BUFFER_DESC_COUNT; idx++) {
  178. desc_list[idx].idx = -1;
  179. desc_list[idx].ptr = NULL;
  180. }
  181. }
  182. /* UBWCP Power control */
  183. static int ubwcp_power(struct ubwcp_driver *ubwcp, bool enable)
  184. {
  185. int ret = 0;
  186. if (enable) {
  187. ret = regulator_enable(ubwcp->vdd);
  188. if (ret < 0) {
  189. ERR("regulator_enable failed: %d", ret);
  190. ret = -1;
  191. } else {
  192. DBG("regulator_enable() success");
  193. }
  194. } else {
  195. ret = regulator_disable(ubwcp->vdd);
  196. if (ret < 0) {
  197. ERR("regulator_disable failed: %d", ret);
  198. ret = -1;
  199. } else {
  200. DBG("regulator_disable() success");
  201. }
  202. }
  203. return ret;
  204. }
  205. static int ubwcp_flush(struct ubwcp_driver *ubwcp)
  206. {
  207. int ret = 0;
  208. mutex_lock(&ubwcp->ubwcp_flush_lock);
  209. ret = ubwcp_hw_flush(ubwcp->base);
  210. mutex_unlock(&ubwcp->ubwcp_flush_lock);
  211. if (ret != 0)
  212. WARN(1, "ubwcp_hw_flush() failed!");
  213. return ret;
  214. }
  215. /* get dma_buf ptr for the given dma_buf fd */
  216. struct dma_buf *ubwcp_dma_buf_fd_to_dma_buf(int dma_buf_fd)
  217. {
  218. struct dma_buf *dmabuf;
  219. /* TBD: dma_buf_get() results in taking ref to buf and it won't ever get
  220. * free'ed until ref count goes to 0. So we must reduce the ref count
  221. * immediately after we find our corresponding ubwcp_buf.
  222. */
  223. dmabuf = dma_buf_get(dma_buf_fd);
  224. if (IS_ERR(dmabuf)) {
  225. ERR("dmabuf ptr not found for dma_buf_fd = %d", dma_buf_fd);
  226. return NULL;
  227. }
  228. dma_buf_put(dmabuf);
  229. return dmabuf;
  230. }
  231. EXPORT_SYMBOL(ubwcp_dma_buf_fd_to_dma_buf);
  232. /* get ubwcp_buf corresponding to the given dma_buf */
  233. static struct ubwcp_buf *dma_buf_to_ubwcp_buf(struct dma_buf *dmabuf)
  234. {
  235. struct ubwcp_buf *buf = NULL;
  236. struct ubwcp_driver *ubwcp = ubwcp_get_driver();
  237. if (!dmabuf || !ubwcp)
  238. return NULL;
  239. mutex_lock(&ubwcp->buf_table_lock);
  240. /* look up ubwcp_buf corresponding to this dma_buf */
  241. hash_for_each_possible(ubwcp->buf_table, buf, hnode, (u64)dmabuf) {
  242. if (buf->dma_buf == dmabuf)
  243. break;
  244. }
  245. mutex_unlock(&ubwcp->buf_table_lock);
  246. return buf;
  247. }
  248. /* return ubwcp hardware version */
  249. int ubwcp_get_hw_version(struct ubwcp_ioctl_hw_version *ver)
  250. {
  251. struct ubwcp_driver *ubwcp;
  252. FENTRY();
  253. if (!ver) {
  254. ERR("invalid version ptr");
  255. return -EINVAL;
  256. }
  257. ubwcp = ubwcp_get_driver();
  258. if (!ubwcp)
  259. return -1;
  260. ver->major = ubwcp->hw_ver_major;
  261. ver->minor = ubwcp->hw_ver_minor;
  262. return 0;
  263. }
  264. EXPORT_SYMBOL(ubwcp_get_hw_version);
  265. /**
  266. *
  267. * Initialize ubwcp buffer for the given dma_buf. This
  268. * initializes ubwcp internal data structures and possibly hw to
  269. * use ubwcp for this buffer.
  270. *
  271. * @param dmabuf : ptr to the buffer to be configured for ubwcp
  272. *
  273. * @return int : 0 on success, otherwise error code
  274. */
  275. static int ubwcp_init_buffer(struct dma_buf *dmabuf)
  276. {
  277. int ret = 0;
  278. int nid;
  279. struct ubwcp_buf *buf;
  280. struct ubwcp_driver *ubwcp = ubwcp_get_driver();
  281. FENTRY();
  282. if (!ubwcp)
  283. return -1;
  284. if (!dmabuf) {
  285. ERR("NULL dmabuf input ptr");
  286. return -EINVAL;
  287. }
  288. if (dma_buf_to_ubwcp_buf(dmabuf)) {
  289. ERR("dma_buf already initialized for ubwcp");
  290. return -EEXIST;
  291. }
  292. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  293. if (!buf) {
  294. ERR("failed to alloc for new ubwcp_buf");
  295. return -ENOMEM;
  296. }
  297. mutex_init(&buf->lock);
  298. buf->dma_buf = dmabuf;
  299. buf->ubwcp = ubwcp;
  300. mutex_lock(&ubwcp->buf_table_lock);
  301. if (hash_empty(ubwcp->buf_table)) {
  302. ret = ubwcp_power(ubwcp, true);
  303. if (ret)
  304. goto err_power_on;
  305. nid = memory_add_physaddr_to_nid(ubwcp->ula_pool_base);
  306. DBG("calling add_memory()...");
  307. ret = add_memory(nid, ubwcp->ula_pool_base, ubwcp->ula_pool_size, MHP_NONE);
  308. if (ret) {
  309. ERR("add_memory() failed st:0x%lx sz:0x%lx err: %d",
  310. ubwcp->ula_pool_base,
  311. ubwcp->ula_pool_size,
  312. ret);
  313. goto err_add_memory;
  314. } else {
  315. DBG("add_memory() ula_pool_base:0x%llx, size:0x%zx, kernel addr:0x%p",
  316. ubwcp->ula_pool_base,
  317. ubwcp->ula_pool_size,
  318. page_to_virt(pfn_to_page(PFN_DOWN(ubwcp->ula_pool_base))));
  319. }
  320. }
  321. hash_add(ubwcp->buf_table, &buf->hnode, (u64)buf->dma_buf);
  322. mutex_unlock(&ubwcp->buf_table_lock);
  323. return ret;
  324. err_add_memory:
  325. ubwcp_power(ubwcp, false);
  326. err_power_on:
  327. mutex_unlock(&ubwcp->buf_table_lock);
  328. kfree(buf);
  329. if (!ret)
  330. ret = -1;
  331. return ret;
  332. }
  333. static void dump_attributes(struct ubwcp_buffer_attrs *attr)
  334. {
  335. DBG_BUF_ATTR("");
  336. DBG_BUF_ATTR("image_format: %d", attr->image_format);
  337. DBG_BUF_ATTR("major_ubwc_ver: %d", attr->major_ubwc_ver);
  338. DBG_BUF_ATTR("minor_ubwc_ver: %d", attr->minor_ubwc_ver);
  339. DBG_BUF_ATTR("compression_type: %d", attr->compression_type);
  340. DBG_BUF_ATTR("lossy_params: %llu", attr->lossy_params);
  341. DBG_BUF_ATTR("width: %d", attr->width);
  342. DBG_BUF_ATTR("height: %d", attr->height);
  343. DBG_BUF_ATTR("stride: %d", attr->stride);
  344. DBG_BUF_ATTR("scanlines: %d", attr->scanlines);
  345. DBG_BUF_ATTR("planar_padding: %d", attr->planar_padding);
  346. DBG_BUF_ATTR("subsample: %d", attr->subsample);
  347. DBG_BUF_ATTR("sub_system_target: %d", attr->sub_system_target);
  348. DBG_BUF_ATTR("y_offset: %d", attr->y_offset);
  349. DBG_BUF_ATTR("batch_size: %d", attr->batch_size);
  350. DBG_BUF_ATTR("");
  351. }
  352. /* validate buffer attributes */
  353. static bool ubwcp_buf_attrs_valid(struct ubwcp_buffer_attrs *attr)
  354. {
  355. bool valid_format;
  356. switch (attr->image_format) {
  357. case UBWCP_LINEAR:
  358. case UBWCP_RGBA8888:
  359. case UBWCP_NV12:
  360. case UBWCP_NV12_Y:
  361. case UBWCP_NV12_UV:
  362. case UBWCP_NV124R:
  363. case UBWCP_NV124R_Y:
  364. case UBWCP_NV124R_UV:
  365. case UBWCP_TP10:
  366. case UBWCP_TP10_Y:
  367. case UBWCP_TP10_UV:
  368. case UBWCP_P010:
  369. case UBWCP_P010_Y:
  370. case UBWCP_P010_UV:
  371. case UBWCP_P016:
  372. case UBWCP_P016_Y:
  373. case UBWCP_P016_UV:
  374. valid_format = true;
  375. break;
  376. default:
  377. valid_format = false;
  378. }
  379. if (!valid_format) {
  380. ERR("invalid image format: %d", attr->image_format);
  381. goto err;
  382. }
  383. if (attr->major_ubwc_ver || attr->minor_ubwc_ver) {
  384. ERR("major/minor ubwc ver must be 0. major: %d minor: %d",
  385. attr->major_ubwc_ver, attr->minor_ubwc_ver);
  386. goto err;
  387. }
  388. if (attr->compression_type != UBWCP_COMPRESSION_LOSSLESS) {
  389. ERR("compression_type is not valid: %d",
  390. attr->compression_type);
  391. goto err;
  392. }
  393. if (attr->lossy_params != 0) {
  394. ERR("lossy_params is not valid: %d", attr->lossy_params);
  395. goto err;
  396. }
  397. //TBD: some upper limit for width?
  398. if (attr->width > 10*1024) {
  399. ERR("width is invalid (above upper limit): %d", attr->width);
  400. goto err;
  401. }
  402. //TBD: some upper limit for height?
  403. if (attr->height > 10*1024) {
  404. ERR("height is invalid (above upper limit): %d", attr->height);
  405. goto err;
  406. }
  407. /* TBD: what's the upper limit for stride? 8K is likely too high. */
  408. if (!IS_ALIGNED(attr->stride, 64) ||
  409. (attr->stride < attr->width) ||
  410. (attr->stride > 4*8192)) {
  411. ERR("stride is not valid (aligned to 64 and <= 8192): %d",
  412. attr->stride);
  413. goto err;
  414. }
  415. /* TBD: currently assume height + 10. Replace 10 with right num from camera. */
  416. if ((attr->scanlines < attr->height) ||
  417. (attr->scanlines > attr->height + 10)) {
  418. ERR("scanlines is not valid - height: %d scanlines: %d",
  419. attr->height, attr->scanlines);
  420. goto err;
  421. }
  422. if (attr->planar_padding > 4096) {
  423. ERR("planar_padding is not valid. (<= 4096): %d",
  424. attr->planar_padding);
  425. goto err;
  426. }
  427. if (attr->subsample != UBWCP_SUBSAMPLE_4_2_0) {
  428. ERR("subsample is not valid: %d", attr->subsample);
  429. goto err;
  430. }
  431. if (attr->sub_system_target & ~UBWCP_SUBSYSTEM_TARGET_CPU) {
  432. ERR("sub_system_target other that CPU is not supported: %d",
  433. attr->sub_system_target);
  434. goto err;
  435. }
  436. if (!(attr->sub_system_target & UBWCP_SUBSYSTEM_TARGET_CPU)) {
  437. ERR("sub_system_target is not set to CPU: %d",
  438. attr->sub_system_target);
  439. goto err;
  440. }
  441. if (attr->y_offset != 0) {
  442. ERR("y_offset is not valid: %d", attr->y_offset);
  443. goto err;
  444. }
  445. if (attr->batch_size != 1) {
  446. ERR("batch_size is not valid: %d", attr->batch_size);
  447. goto err;
  448. }
  449. dump_attributes(attr);
  450. return true;
  451. err:
  452. dump_attributes(attr);
  453. return false;
  454. }
  455. /* return true if image format has only Y plane*/
  456. bool ubwcp_image_y_only(u16 format)
  457. {
  458. switch (format) {
  459. case UBWCP_NV12_Y:
  460. case UBWCP_NV124R_Y:
  461. case UBWCP_TP10_Y:
  462. case UBWCP_P010_Y:
  463. case UBWCP_P016_Y:
  464. return true;
  465. default:
  466. return false;
  467. }
  468. }
  469. /* return true if image format has only UV plane*/
  470. bool ubwcp_image_uv_only(u16 format)
  471. {
  472. switch (format) {
  473. case UBWCP_NV12_UV:
  474. case UBWCP_NV124R_UV:
  475. case UBWCP_TP10_UV:
  476. case UBWCP_P010_UV:
  477. case UBWCP_P016_UV:
  478. return true;
  479. default:
  480. return false;
  481. }
  482. }
  483. /* calculate and return metadata buffer size for a given plane
  484. * and buffer attributes
  485. * NOTE: in this function, we will only pass in NV12 format.
  486. * NOT NV12_Y or NV12_UV etc.
  487. * the Y or UV information is in the "plane"
  488. * "format" here purely means "encoding format" and no information
  489. * if some plane data is missing.
  490. */
  491. static size_t metadata_buf_sz(struct ubwcp_driver *ubwcp,
  492. enum ubwcp_std_image_format format,
  493. u32 width, u32 height, u8 plane)
  494. {
  495. size_t size;
  496. u64 pitch;
  497. u64 lines;
  498. u64 tile_width;
  499. u32 tile_height;
  500. struct ubwcp_image_format_info f_info;
  501. struct ubwcp_plane_info p_info;
  502. f_info = ubwcp->format_info[format];
  503. DBG_BUF_ATTR("");
  504. DBG_BUF_ATTR("");
  505. DBG_BUF_ATTR("Calculating metadata buffer size: format = %d, plane = %d", format, plane);
  506. if (plane >= f_info.planes) {
  507. ERR("Format does not have requested plane info: format: %d, plane: %d",
  508. format, plane);
  509. WARN(1, "Fix this!!!!!");
  510. return 0;
  511. }
  512. p_info = f_info.p_info[plane];
  513. /* UV plane */
  514. if (plane == 1) {
  515. width = width/2;
  516. height = height/2;
  517. }
  518. tile_width = p_info.tilesize_p.width;
  519. tile_height = p_info.tilesize_p.height;
  520. /* pitch: # of tiles in a row
  521. * lines: # of tile rows
  522. */
  523. pitch = UBWCP_ALIGN((width + tile_width - 1)/tile_width, META_DATA_PITCH_ALIGN);
  524. lines = UBWCP_ALIGN((height + tile_height - 1)/tile_height, META_DATA_HEIGHT_ALIGN);
  525. DBG_BUF_ATTR("image params : %d x %d (pixels)", width, height);
  526. DBG_BUF_ATTR("tile params : %d x %d (pixels)", tile_width, tile_height);
  527. DBG_BUF_ATTR("pitch : %d (%d)", pitch, width/tile_width);
  528. DBG_BUF_ATTR("lines : %d (%d)", lines, height);
  529. DBG_BUF_ATTR("size (p*l*bytes) : %d", pitch*lines*1);
  530. /* x1 below is only to clarify that we are multiplying by 1 bytes/tile */
  531. size = UBWCP_ALIGN(pitch*lines*1, META_DATA_SIZE_ALIGN);
  532. DBG_BUF_ATTR("size (aligned 4K): %zu (0x%zx)", size, size);
  533. return size;
  534. }
  535. /* calculate and return size of pixel data buffer for a given plane
  536. * and buffer attributes
  537. */
  538. static size_t pixeldata_buf_sz(struct ubwcp_driver *ubwcp,
  539. u16 format, u32 width,
  540. u32 height, u8 plane)
  541. {
  542. size_t size;
  543. u64 pitch;
  544. u64 lines;
  545. u16 pixel_bytes;
  546. u16 per_pixel;
  547. u64 macro_tile_width_p;
  548. u64 macro_tile_height_p;
  549. struct ubwcp_image_format_info f_info;
  550. struct ubwcp_plane_info p_info;
  551. f_info = ubwcp->format_info[format];
  552. DBG_BUF_ATTR("");
  553. DBG_BUF_ATTR("");
  554. DBG_BUF_ATTR("Calculating Pixeldata buffer size: format = %d, plane = %d", format, plane);
  555. if (plane >= f_info.planes) {
  556. ERR("Format does not have requested plane info: format: %d, plane: %d",
  557. format, plane);
  558. WARN(1, "Fix this!!!!!");
  559. return 0;
  560. }
  561. p_info = f_info.p_info[plane];
  562. pixel_bytes = p_info.pixel_bytes;
  563. per_pixel = p_info.per_pixel;
  564. /* UV plane */
  565. if (plane == 1) {
  566. width = width/2;
  567. height = height/2;
  568. }
  569. macro_tile_width_p = p_info.macrotilesize_p.width;
  570. macro_tile_height_p = p_info.macrotilesize_p.height;
  571. /* align pixel width and height macro tile width and height */
  572. pitch = UBWCP_ALIGN(width, macro_tile_width_p);
  573. lines = UBWCP_ALIGN(height, macro_tile_height_p);
  574. DBG_BUF_ATTR("image params : %d x %d (pixels)", width, height);
  575. DBG_BUF_ATTR("macro tile params: %d x %d (pixels)", macro_tile_width_p,
  576. macro_tile_height_p);
  577. DBG_BUF_ATTR("bytes_per_pixel : %d/%d", pixel_bytes, per_pixel);
  578. DBG_BUF_ATTR("pitch : %d", pitch);
  579. DBG_BUF_ATTR("lines : %d", lines);
  580. DBG_BUF_ATTR("size (p*l*bytes) : %d", (pitch*lines*pixel_bytes)/per_pixel);
  581. size = UBWCP_ALIGN((pitch*lines*pixel_bytes)/per_pixel, PIXEL_DATA_SIZE_ALIGN);
  582. DBG_BUF_ATTR("size (aligned 4K): %zu (0x%zx)", size, size);
  583. return size;
  584. }
  585. /*
  586. * plane: must be 0 or 1 (1st plane == 0, 2nd plane == 1)
  587. */
  588. static size_t ubwcp_ula_size(struct ubwcp_driver *ubwcp, u16 format,
  589. u32 stride_b, u32 scanlines, u8 plane)
  590. {
  591. size_t size;
  592. DBG_BUF_ATTR("%s(format = %d, plane = %d)", __func__, format, plane);
  593. /* UV plane */
  594. if (plane == 1)
  595. scanlines = scanlines/2;
  596. size = stride_b*scanlines;
  597. DBG_BUF_ATTR("Size of plane-%u: (%u * %u) = %zu (0x%zx)",
  598. plane, stride_b, scanlines, size, size);
  599. return size;
  600. }
  601. int missing_plane_from_format(u16 ioctl_image_format)
  602. {
  603. int missing_plane;
  604. switch (ioctl_image_format) {
  605. case UBWCP_NV12_Y:
  606. missing_plane = 2;
  607. break;
  608. case UBWCP_NV12_UV:
  609. missing_plane = 1;
  610. break;
  611. case UBWCP_NV124R_Y:
  612. missing_plane = 2;
  613. break;
  614. case UBWCP_NV124R_UV:
  615. missing_plane = 1;
  616. break;
  617. case UBWCP_TP10_Y:
  618. missing_plane = 2;
  619. break;
  620. case UBWCP_TP10_UV:
  621. missing_plane = 1;
  622. break;
  623. case UBWCP_P010_Y:
  624. missing_plane = 2;
  625. break;
  626. case UBWCP_P010_UV:
  627. missing_plane = 1;
  628. break;
  629. case UBWCP_P016_Y:
  630. missing_plane = 2;
  631. break;
  632. case UBWCP_P016_UV:
  633. missing_plane = 1;
  634. break;
  635. default:
  636. missing_plane = 0;
  637. }
  638. return missing_plane;
  639. }
  640. int planes_in_format(enum ubwcp_std_image_format format)
  641. {
  642. if (format == RGBA)
  643. return 1;
  644. else
  645. return 2;
  646. }
  647. enum ubwcp_std_image_format to_std_format(u16 ioctl_image_format)
  648. {
  649. switch (ioctl_image_format) {
  650. case UBWCP_RGBA8888:
  651. return RGBA;
  652. case UBWCP_NV12:
  653. case UBWCP_NV12_Y:
  654. case UBWCP_NV12_UV:
  655. return NV12;
  656. case UBWCP_NV124R:
  657. case UBWCP_NV124R_Y:
  658. case UBWCP_NV124R_UV:
  659. return NV124R;
  660. case UBWCP_TP10:
  661. case UBWCP_TP10_Y:
  662. case UBWCP_TP10_UV:
  663. return TP10;
  664. case UBWCP_P010:
  665. case UBWCP_P010_Y:
  666. case UBWCP_P010_UV:
  667. return P010;
  668. case UBWCP_P016:
  669. case UBWCP_P016_Y:
  670. case UBWCP_P016_UV:
  671. return P016;
  672. default:
  673. WARN(1, "Fix this!!!");
  674. return STD_IMAGE_FORMAT_INVALID;
  675. }
  676. }
  677. unsigned int ubwcp_get_hw_image_format_value(u16 ioctl_image_format)
  678. {
  679. enum ubwcp_std_image_format format;
  680. format = to_std_format(ioctl_image_format);
  681. switch (format) {
  682. case RGBA:
  683. return HW_BUFFER_FORMAT_RGBA;
  684. case NV12:
  685. return HW_BUFFER_FORMAT_NV12;
  686. case NV124R:
  687. return HW_BUFFER_FORMAT_NV124R;
  688. case P010:
  689. return HW_BUFFER_FORMAT_P010;
  690. case TP10:
  691. return HW_BUFFER_FORMAT_TP10;
  692. case P016:
  693. return HW_BUFFER_FORMAT_P016;
  694. default:
  695. WARN(1, "Fix this!!!!!");
  696. return 0;
  697. }
  698. }
  699. /* calculate ULA buffer parms
  700. * TBD: how do we make sure uv_start address (not the offset)
  701. * is aligned per requirement: cache line
  702. */
  703. static int ubwcp_calc_ula_params(struct ubwcp_driver *ubwcp,
  704. struct ubwcp_buffer_attrs *attr,
  705. size_t *ula_size,
  706. size_t *uv_start_offset)
  707. {
  708. size_t size;
  709. enum ubwcp_std_image_format format;
  710. int planes;
  711. int missing_plane;
  712. u32 stride;
  713. u32 scanlines;
  714. u32 planar_padding;
  715. stride = attr->stride;
  716. scanlines = attr->scanlines;
  717. planar_padding = attr->planar_padding;
  718. /* convert ioctl image format to standard image format */
  719. format = to_std_format(attr->image_format);
  720. /* Number of "expected" planes in "the standard defined" image format */
  721. planes = planes_in_format(format);
  722. /* any plane missing?
  723. * valid missing_plane values:
  724. * 0 == no plane missing
  725. * 1 == 1st plane missing
  726. * 2 == 2nd plane missing
  727. */
  728. missing_plane = missing_plane_from_format(attr->image_format);
  729. DBG_BUF_ATTR("ioctl_image_format : %d, std_format: %d", attr->image_format, format);
  730. DBG_BUF_ATTR("planes_in_format : %d", planes);
  731. DBG_BUF_ATTR("missing_plane : %d", missing_plane);
  732. DBG_BUF_ATTR("Planar Padding : %d", planar_padding);
  733. if (planes == 1) {
  734. /* uv_start beyond ULA range */
  735. size = ubwcp_ula_size(ubwcp, format, stride, scanlines, 0);
  736. *uv_start_offset = size;
  737. } else {
  738. if (!missing_plane) {
  739. /* size for both planes and padding */
  740. size = ubwcp_ula_size(ubwcp, format, stride, scanlines, 0);
  741. size += planar_padding;
  742. *uv_start_offset = size;
  743. size += ubwcp_ula_size(ubwcp, format, stride, scanlines, 1);
  744. } else {
  745. if (missing_plane == 2) {
  746. /* Y-only image, set uv_start beyond ULA range */
  747. size = ubwcp_ula_size(ubwcp, format, stride, scanlines, 0);
  748. *uv_start_offset = size;
  749. } else {
  750. /* first plane data is not there */
  751. size = ubwcp_ula_size(ubwcp, format, stride, scanlines, 1);
  752. *uv_start_offset = 0; /* uv data is at the beginning */
  753. }
  754. }
  755. }
  756. //TBD: cleanup
  757. *ula_size = size;
  758. DBG_BUF_ATTR("Before page align: Total ULA_Size: %d (0x%x) (planes + planar padding)",
  759. *ula_size, *ula_size);
  760. *ula_size = UBWCP_ALIGN(size, 4096);
  761. DBG_BUF_ATTR("After page align : Total ULA_Size: %d (0x%x) (planes + planar padding)",
  762. *ula_size, *ula_size);
  763. return 0;
  764. }
  765. /* calculate UBWCP buffer parms */
  766. static int ubwcp_calc_ubwcp_buf_params(struct ubwcp_driver *ubwcp,
  767. struct ubwcp_buffer_attrs *attr,
  768. size_t *md_p0, size_t *pd_p0,
  769. size_t *md_p1, size_t *pd_p1,
  770. size_t *stride_tp10_b)
  771. {
  772. int planes;
  773. int missing_plane;
  774. enum ubwcp_std_image_format format;
  775. size_t stride_tp10_p;
  776. FENTRY();
  777. /* convert ioctl image format to standard image format */
  778. format = to_std_format(attr->image_format);
  779. missing_plane = missing_plane_from_format(attr->image_format);
  780. planes = planes_in_format(format); //pass in 0 (RGB) should return 1
  781. DBG_BUF_ATTR("ioctl_image_format : %d, std_format: %d", attr->image_format, format);
  782. DBG_BUF_ATTR("planes_in_format : %d", planes);
  783. DBG_BUF_ATTR("missing_plane : %d", missing_plane);
  784. if (!missing_plane) {
  785. *md_p0 = metadata_buf_sz(ubwcp, format, attr->width, attr->height, 0);
  786. *pd_p0 = pixeldata_buf_sz(ubwcp, format, attr->width, attr->height, 0);
  787. if (planes == 2) {
  788. *md_p1 = metadata_buf_sz(ubwcp, format, attr->width, attr->height, 1);
  789. *pd_p1 = pixeldata_buf_sz(ubwcp, format, attr->width, attr->height, 1);
  790. }
  791. } else {
  792. if (missing_plane == 1) {
  793. *md_p0 = 0;
  794. *pd_p0 = 0;
  795. *md_p1 = metadata_buf_sz(ubwcp, format, attr->width, attr->height, 1);
  796. *pd_p1 = pixeldata_buf_sz(ubwcp, format, attr->width, attr->height, 1);
  797. } else {
  798. *md_p0 = metadata_buf_sz(ubwcp, format, attr->width, attr->height, 0);
  799. *pd_p0 = pixeldata_buf_sz(ubwcp, format, attr->width, attr->height, 0);
  800. *md_p1 = 0;
  801. *pd_p1 = 0;
  802. }
  803. }
  804. if (format == TP10) {
  805. stride_tp10_p = UBWCP_ALIGN(attr->width, 192);
  806. *stride_tp10_b = (stride_tp10_p/3) + stride_tp10_p;
  807. } else {
  808. *stride_tp10_b = 0;
  809. }
  810. return 0;
  811. }
  812. /* reserve ULA address space of the given size */
  813. static phys_addr_t ubwcp_ula_alloc(struct ubwcp_driver *ubwcp, size_t size)
  814. {
  815. phys_addr_t pa;
  816. mutex_lock(&ubwcp->ula_lock);
  817. pa = gen_pool_alloc(ubwcp->ula_pool, size);
  818. DBG("addr: %p, size: %zx", pa, size);
  819. mutex_unlock(&ubwcp->ula_lock);
  820. return pa;
  821. }
  822. /* free ULA address space of the given address and size */
  823. static void ubwcp_ula_free(struct ubwcp_driver *ubwcp, phys_addr_t pa, size_t size)
  824. {
  825. mutex_lock(&ubwcp->ula_lock);
  826. if (!gen_pool_has_addr(ubwcp->ula_pool, pa, size)) {
  827. ERR("Attempt to free mem not from gen_pool: pa: %p, size: %zx", pa, size);
  828. goto err;
  829. }
  830. DBG("addr: %p, size: %zx", pa, size);
  831. gen_pool_free(ubwcp->ula_pool, pa, size);
  832. mutex_unlock(&ubwcp->ula_lock);
  833. return;
  834. err:
  835. mutex_unlock(&ubwcp->ula_lock);
  836. }
  837. /* free up or expand current_pa and return the new pa */
  838. static phys_addr_t ubwcp_ula_realloc(struct ubwcp_driver *ubwcp,
  839. phys_addr_t pa,
  840. size_t size,
  841. size_t new_size)
  842. {
  843. if (size == new_size)
  844. return pa;
  845. if (pa)
  846. ubwcp_ula_free(ubwcp, pa, size);
  847. return ubwcp_ula_alloc(ubwcp, new_size);
  848. }
  849. /* unmap dma buf */
  850. static void ubwcp_dma_unmap(struct ubwcp_buf *buf)
  851. {
  852. FENTRY();
  853. if (buf->dma_buf && buf->attachment) {
  854. DBG("Calling dma_buf_unmap_attachment()");
  855. dma_buf_unmap_attachment(buf->attachment, buf->sgt, DMA_BIDIRECTIONAL);
  856. buf->sgt = NULL;
  857. dma_buf_detach(buf->dma_buf, buf->attachment);
  858. buf->attachment = NULL;
  859. }
  860. }
  861. /* dma map ubwcp buffer */
  862. static int ubwcp_dma_map(struct ubwcp_buf *buf,
  863. struct device *dev,
  864. size_t iova_min_size,
  865. dma_addr_t *iova)
  866. {
  867. int ret = 0;
  868. struct dma_buf *dma_buf = buf->dma_buf;
  869. struct dma_buf_attachment *attachment;
  870. struct sg_table *sgt;
  871. size_t dma_len;
  872. /* Map buffer to SMMU and get IOVA */
  873. attachment = dma_buf_attach(dma_buf, dev);
  874. if (IS_ERR(attachment)) {
  875. ret = PTR_ERR(attachment);
  876. ERR("dma_buf_attach() failed: %d", ret);
  877. goto err;
  878. }
  879. dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
  880. dma_set_seg_boundary(dev, (unsigned long)DMA_BIT_MASK(64));
  881. sgt = dma_buf_map_attachment(attachment, DMA_BIDIRECTIONAL);
  882. if (IS_ERR_OR_NULL(sgt)) {
  883. ret = PTR_ERR(sgt);
  884. ERR("dma_buf_map_attachment() failed: %d", ret);
  885. goto err_detach;
  886. }
  887. if (sgt->nents != 1) {
  888. ERR("nents = %d", sgt->nents);
  889. goto err_unmap;
  890. }
  891. /* ensure that dma_buf is big enough for the new attrs */
  892. dma_len = sg_dma_len(sgt->sgl);
  893. if (dma_len < iova_min_size) {
  894. ERR("dma len: %d is less than min ubwcp buffer size: %d",
  895. dma_len, iova_min_size);
  896. goto err_unmap;
  897. }
  898. *iova = sg_dma_address(sgt->sgl);
  899. buf->attachment = attachment;
  900. buf->sgt = sgt;
  901. return ret;
  902. err_unmap:
  903. dma_buf_unmap_attachment(attachment, sgt, DMA_BIDIRECTIONAL);
  904. err_detach:
  905. dma_buf_detach(dma_buf, attachment);
  906. err:
  907. if (!ret)
  908. ret = -1;
  909. return ret;
  910. }
  911. static void
  912. ubwcp_pixel_to_bytes(struct ubwcp_driver *ubwcp,
  913. enum ubwcp_std_image_format format,
  914. u32 width_p, u32 height_p,
  915. u32 *width_b, u32 *height_b)
  916. {
  917. u16 pixel_bytes;
  918. u16 per_pixel;
  919. struct ubwcp_image_format_info f_info;
  920. struct ubwcp_plane_info p_info;
  921. f_info = ubwcp->format_info[format];
  922. p_info = f_info.p_info[0];
  923. pixel_bytes = p_info.pixel_bytes;
  924. per_pixel = p_info.per_pixel;
  925. *width_b = (width_p*pixel_bytes)/per_pixel;
  926. *height_b = (height_p*pixel_bytes)/per_pixel;
  927. }
  928. static void reset_buf_attrs(struct ubwcp_buf *buf)
  929. {
  930. struct ubwcp_hw_meta_metadata *mmdata;
  931. struct ubwcp_driver *ubwcp;
  932. ubwcp = buf->ubwcp;
  933. mmdata = &buf->mmdata;
  934. ubwcp_dma_unmap(buf);
  935. /* reset ula params */
  936. if (buf->ula_size) {
  937. ubwcp_ula_free(ubwcp, buf->ula_pa, buf->ula_size);
  938. buf->ula_size = 0;
  939. buf->ula_pa = 0;
  940. }
  941. /* reset ubwcp params */
  942. memset(mmdata, 0, sizeof(*mmdata));
  943. buf->buf_attr_set = false;
  944. }
  945. static void print_mmdata_desc(struct ubwcp_hw_meta_metadata *mmdata)
  946. {
  947. DBG_BUF_ATTR("");
  948. DBG_BUF_ATTR("--------MM_DATA DESC ---------");
  949. DBG_BUF_ATTR("uv_start_addr : 0x%08llx (cache addr) (actual: 0x%llx)",
  950. mmdata->uv_start_addr, mmdata->uv_start_addr << 6);
  951. DBG_BUF_ATTR("format : 0x%08x", mmdata->format);
  952. DBG_BUF_ATTR("stride : 0x%08x (cache addr) (actual: 0x%x)",
  953. mmdata->stride, mmdata->stride << 6);
  954. DBG_BUF_ATTR("stride_ubwcp : 0x%08x (cache addr) (actual: 0x%zx)",
  955. mmdata->stride_ubwcp, mmdata->stride_ubwcp << 6);
  956. DBG_BUF_ATTR("metadata_base_y : 0x%08x (page addr) (actual: 0x%llx)",
  957. mmdata->metadata_base_y, mmdata->metadata_base_y << 12);
  958. DBG_BUF_ATTR("metadata_base_uv: 0x%08x (page addr) (actual: 0x%zx)",
  959. mmdata->metadata_base_uv, mmdata->metadata_base_uv << 12);
  960. DBG_BUF_ATTR("buffer_y_offset : 0x%08x (page addr) (actual: 0x%zx)",
  961. mmdata->buffer_y_offset, mmdata->buffer_y_offset << 12);
  962. DBG_BUF_ATTR("buffer_uv_offset: 0x%08x (page addr) (actual: 0x%zx)",
  963. mmdata->buffer_uv_offset, mmdata->buffer_uv_offset << 12);
  964. DBG_BUF_ATTR("width_height : 0x%08x (width: 0x%x height: 0x%x)",
  965. mmdata->width_height, mmdata->width_height >> 16, mmdata->width_height & 0xFFFF);
  966. DBG_BUF_ATTR("");
  967. }
  968. /* set buffer attributes:
  969. * Failure:
  970. * If a call to ubwcp_set_buf_attrs() fails, any attributes set from a previously
  971. * successful ubwcp_set_buf_attrs() will be also removed. Thus,
  972. * ubwcp_set_buf_attrs() implicitly does "unset previous attributes" and
  973. * then "try to set these new attributes".
  974. *
  975. * The result of a failed call to ubwcp_set_buf_attrs() will leave the buffer
  976. * in a linear mode, NOT with attributes from earlier successful call.
  977. */
  978. int ubwcp_set_buf_attrs(struct dma_buf *dmabuf, struct ubwcp_buffer_attrs *attr)
  979. {
  980. int ret = 0;
  981. size_t ula_size = 0;
  982. size_t uv_start_offset = 0;
  983. phys_addr_t ula_pa = 0x0;
  984. struct ubwcp_buf *buf;
  985. struct ubwcp_driver *ubwcp;
  986. size_t metadata_p0;
  987. size_t pixeldata_p0;
  988. size_t metadata_p1;
  989. size_t pixeldata_p1;
  990. size_t iova_min_size;
  991. size_t stride_tp10_b;
  992. dma_addr_t iova_base;
  993. struct ubwcp_hw_meta_metadata *mmdata;
  994. u64 uv_start;
  995. u32 stride_b;
  996. u32 width_b;
  997. u32 height_b;
  998. enum ubwcp_std_image_format std_image_format;
  999. FENTRY();
  1000. if (!dmabuf) {
  1001. ERR("NULL dmabuf input ptr");
  1002. return -EINVAL;
  1003. }
  1004. if (!attr) {
  1005. ERR("NULL attr ptr");
  1006. return -EINVAL;
  1007. }
  1008. buf = dma_buf_to_ubwcp_buf(dmabuf);
  1009. if (!buf) {
  1010. ERR("No corresponding ubwcp_buf for the passed in dma_buf");
  1011. return -EINVAL;
  1012. }
  1013. mutex_lock(&buf->lock);
  1014. if (buf->locked) {
  1015. ERR("Cannot set attr when buffer is locked");
  1016. ret = -EBUSY;
  1017. goto err;
  1018. }
  1019. ubwcp = buf->ubwcp;
  1020. mmdata = &buf->mmdata;
  1021. //TBD: now that we have single exit point for all errors,
  1022. //we can limit this call to error only?
  1023. //also see if this can be part of reset_buf_attrs()
  1024. DBG_BUF_ATTR("resetting mmap to linear");
  1025. /* remove any earlier dma buf mmap configuration */
  1026. ret = ubwcp->mmap_config_fptr(buf->dma_buf, true, 0, 0);
  1027. if (ret) {
  1028. ERR("dma_buf_mmap_config() failed: %d", ret);
  1029. goto err;
  1030. }
  1031. if (!ubwcp_buf_attrs_valid(attr)) {
  1032. ERR("Invalid buf attrs");
  1033. goto err;
  1034. }
  1035. DBG_BUF_ATTR("valid buf attrs");
  1036. if (attr->image_format == UBWCP_LINEAR) {
  1037. DBG_BUF_ATTR("Linear format requested");
  1038. /* linear format request with permanent range xlation doesn't
  1039. * make sense. need to define behavior if this happens.
  1040. * note: with perm set, desc is allocated to this buffer.
  1041. */
  1042. //TBD: UBWCP_ASSERT(!buf->perm);
  1043. if (buf->buf_attr_set)
  1044. reset_buf_attrs(buf);
  1045. mutex_unlock(&buf->lock);
  1046. return 0;
  1047. }
  1048. std_image_format = to_std_format(attr->image_format);
  1049. if (std_image_format == STD_IMAGE_FORMAT_INVALID) {
  1050. ERR("Unable to map ioctl image format to std image format");
  1051. goto err;
  1052. }
  1053. /* Calculate uncompressed-buffer size. */
  1054. DBG_BUF_ATTR("");
  1055. DBG_BUF_ATTR("");
  1056. DBG_BUF_ATTR("Calculating ula params -->");
  1057. ret = ubwcp_calc_ula_params(ubwcp, attr, &ula_size, &uv_start_offset);
  1058. if (ret) {
  1059. ERR("ubwcp_calc_ula_params() failed: %d", ret);
  1060. goto err;
  1061. }
  1062. DBG_BUF_ATTR("");
  1063. DBG_BUF_ATTR("");
  1064. DBG_BUF_ATTR("Calculating ubwcp params -->");
  1065. ret = ubwcp_calc_ubwcp_buf_params(ubwcp, attr,
  1066. &metadata_p0, &pixeldata_p0,
  1067. &metadata_p1, &pixeldata_p1,
  1068. &stride_tp10_b);
  1069. if (ret) {
  1070. ERR("ubwcp_calc_buf_params() failed: %d", ret);
  1071. goto err;
  1072. }
  1073. iova_min_size = metadata_p0 + pixeldata_p0 + metadata_p1 + pixeldata_p1;
  1074. DBG_BUF_ATTR("");
  1075. DBG_BUF_ATTR("");
  1076. DBG_BUF_ATTR("------Summary ULA Calculated Params ------");
  1077. DBG_BUF_ATTR("ULA Size : %8zu (0x%8zx)", ula_size, ula_size);
  1078. DBG_BUF_ATTR("UV Start Offset : %8zu (0x%8zx)", uv_start_offset, uv_start_offset);
  1079. DBG_BUF_ATTR("------Summary UBCP Calculated Params ------");
  1080. DBG_BUF_ATTR("metadata_p0 : %8d (0x%8zx)", metadata_p0, metadata_p0);
  1081. DBG_BUF_ATTR("pixeldata_p0 : %8d (0x%8zx)", pixeldata_p0, pixeldata_p0);
  1082. DBG_BUF_ATTR("metadata_p1 : %8d (0x%8zx)", metadata_p1, metadata_p1);
  1083. DBG_BUF_ATTR("pixeldata_p1 : %8d (0x%8zx)", pixeldata_p1, pixeldata_p1);
  1084. DBG_BUF_ATTR("stride_tp10 : %8d (0x%8zx)", stride_tp10_b, stride_tp10_b);
  1085. DBG_BUF_ATTR("iova_min_size : %8d (0x%8zx)", iova_min_size, iova_min_size);
  1086. DBG_BUF_ATTR("");
  1087. if (buf->buf_attr_set) {
  1088. /* if buf attr were previously set, these must not be 0 */
  1089. /* TBD: do we need this check in production code? */
  1090. if (!buf->ula_pa) {
  1091. WARN(1, "ula_pa cannot be 0 if buf_attr_set is true!!!");
  1092. goto err;
  1093. }
  1094. if (!buf->ula_size) {
  1095. WARN(1, "ula_size cannot be 0 if buf_attr_set is true!!!");
  1096. goto err;
  1097. }
  1098. }
  1099. /* assign ULA PA with uncompressed-size range */
  1100. ula_pa = ubwcp_ula_realloc(ubwcp, buf->ula_pa, buf->ula_size, ula_size);
  1101. if (!ula_pa) {
  1102. ERR("ubwcp_ula_alloc/realloc() failed. running out of ULA PA space?");
  1103. goto err;
  1104. }
  1105. buf->ula_size = ula_size;
  1106. buf->ula_pa = ula_pa;
  1107. DBG_BUF_ATTR("Allocated ULA_PA: 0x%p of size: 0x%zx", ula_pa, ula_size);
  1108. DBG_BUF_ATTR("");
  1109. /* inform ULA-PA to dma-heap: needed for dma-heap to do CMOs later on */
  1110. DBG_BUF_ATTR("Calling mmap_config(): ULA_PA: 0x%p size: 0x%zx", ula_pa, ula_size);
  1111. ret = ubwcp->mmap_config_fptr(buf->dma_buf, false, buf->ula_pa,
  1112. buf->ula_size);
  1113. if (ret) {
  1114. ERR("dma_buf_mmap_config() failed: %d", ret);
  1115. goto err;
  1116. }
  1117. /* dma map only the first time attribute is set */
  1118. if (!buf->buf_attr_set) {
  1119. /* linear -> ubwcp. map ubwcp buffer */
  1120. ret = ubwcp_dma_map(buf, ubwcp->dev_buf_cb, iova_min_size, &iova_base);
  1121. if (ret) {
  1122. ERR("ubwcp_dma_map() failed: %d", ret);
  1123. goto err;
  1124. }
  1125. DBG_BUF_ATTR("dma_buf IOVA range: 0x%llx + min_size (0x%zx): 0x%llx",
  1126. iova_base, iova_min_size, iova_base + iova_min_size);
  1127. }
  1128. uv_start = ula_pa + uv_start_offset;
  1129. if (!IS_ALIGNED(uv_start, 64)) {
  1130. ERR("ERROR: uv_start is NOT aligned to cache line");
  1131. goto err;
  1132. }
  1133. /* Convert height and width to bytes for writing to mmdata */
  1134. if (std_image_format != TP10) {
  1135. ubwcp_pixel_to_bytes(ubwcp, std_image_format, attr->width,
  1136. attr->height, &width_b, &height_b);
  1137. } else {
  1138. /* for tp10 image compression, we need to program p010 width/height */
  1139. ubwcp_pixel_to_bytes(ubwcp, P010, attr->width,
  1140. attr->height, &width_b, &height_b);
  1141. }
  1142. stride_b = attr->stride;
  1143. /* create the mmdata descriptor */
  1144. memset(mmdata, 0, sizeof(*mmdata));
  1145. mmdata->uv_start_addr = CACHE_ADDR(uv_start);
  1146. mmdata->format = ubwcp_get_hw_image_format_value(attr->image_format);
  1147. if (std_image_format != TP10) {
  1148. mmdata->stride = CACHE_ADDR(stride_b); /* uncompressed stride */
  1149. } else {
  1150. mmdata->stride = CACHE_ADDR(stride_tp10_b); /* compressed stride */
  1151. mmdata->stride_ubwcp = CACHE_ADDR(stride_b); /* uncompressed stride */
  1152. }
  1153. mmdata->metadata_base_y = PAGE_ADDR(iova_base);
  1154. mmdata->metadata_base_uv = PAGE_ADDR(iova_base + metadata_p0 + pixeldata_p0);
  1155. mmdata->buffer_y_offset = PAGE_ADDR(metadata_p0);
  1156. mmdata->buffer_uv_offset = PAGE_ADDR(metadata_p1);
  1157. mmdata->width_height = width_b << 16 | height_b;
  1158. print_mmdata_desc(mmdata);
  1159. buf->buf_attr = *attr;
  1160. buf->buf_attr_set = true;
  1161. //TBD: UBWCP_ASSERT(!buf->perm);
  1162. mutex_unlock(&buf->lock);
  1163. return 0;
  1164. err:
  1165. reset_buf_attrs(buf);
  1166. mutex_unlock(&buf->lock);
  1167. if (!ret)
  1168. ret = -1;
  1169. return ret;
  1170. }
  1171. EXPORT_SYMBOL(ubwcp_set_buf_attrs);
  1172. /* Set buffer attributes ioctl */
  1173. static int ubwcp_set_buf_attrs_ioctl(struct ubwcp_ioctl_buffer_attrs *attr_ioctl)
  1174. {
  1175. struct dma_buf *dmabuf;
  1176. dmabuf = ubwcp_dma_buf_fd_to_dma_buf(attr_ioctl->fd);
  1177. return ubwcp_set_buf_attrs(dmabuf, &attr_ioctl->attr);
  1178. }
  1179. /* Free up the buffer descriptor */
  1180. static void ubwcp_buf_desc_free(struct ubwcp_driver *ubwcp, struct ubwcp_desc *desc)
  1181. {
  1182. int idx = desc->idx;
  1183. struct ubwcp_desc *desc_list = ubwcp->desc_list;
  1184. mutex_lock(&ubwcp->desc_lock);
  1185. desc_list[idx].idx = -1;
  1186. desc_list[idx].ptr = NULL;
  1187. DBG("freed descriptor_id: %d", idx);
  1188. mutex_unlock(&ubwcp->desc_lock);
  1189. }
  1190. /* Allocate next available buffer descriptor. */
  1191. static struct ubwcp_desc *ubwcp_buf_desc_allocate(struct ubwcp_driver *ubwcp)
  1192. {
  1193. int idx;
  1194. struct ubwcp_desc *desc_list = ubwcp->desc_list;
  1195. mutex_lock(&ubwcp->desc_lock);
  1196. for (idx = 0; idx < UBWCP_BUFFER_DESC_COUNT; idx++) {
  1197. if (desc_list[idx].idx == -1) {
  1198. desc_list[idx].idx = idx;
  1199. desc_list[idx].ptr = ubwcp->buffer_desc_base +
  1200. idx*UBWCP_BUFFER_DESC_OFFSET;
  1201. DBG("allocated descriptor_id: %d", idx);
  1202. mutex_unlock(&ubwcp->desc_lock);
  1203. return &desc_list[idx];
  1204. }
  1205. }
  1206. mutex_unlock(&ubwcp->desc_lock);
  1207. return NULL;
  1208. }
  1209. #define FLUSH_WA_SIZE 64
  1210. #define FLUSH_WA_UDELAY 89
  1211. void ubwcp_flush_cache_wa(struct device *dev, phys_addr_t paddr, size_t size)
  1212. {
  1213. phys_addr_t cline = paddr;
  1214. int num_line = size / FLUSH_WA_SIZE;
  1215. int i;
  1216. for (i = 0; i < num_line; i++) {
  1217. dma_sync_single_for_cpu(dev, cline, FLUSH_WA_SIZE, 0);
  1218. udelay(FLUSH_WA_UDELAY);
  1219. cline += FLUSH_WA_SIZE;
  1220. }
  1221. }
  1222. /**
  1223. * Lock buffer for CPU access. This prepares ubwcp hw to allow
  1224. * CPU access to the compressed buffer. It will perform
  1225. * necessary address translation configuration and cache maintenance ops
  1226. * so that CPU can safely access ubwcp buffer, if this call is
  1227. * successful.
  1228. * Allocate descriptor if not already,
  1229. * perform CMO and then enable range check
  1230. *
  1231. * @param dmabuf : ptr to the dma buf
  1232. * @param direction : direction of access
  1233. *
  1234. * @return int : 0 on success, otherwise error code
  1235. */
  1236. static int ubwcp_lock(struct dma_buf *dmabuf, enum dma_data_direction dir)
  1237. {
  1238. int ret = 0;
  1239. struct ubwcp_buf *buf;
  1240. struct ubwcp_driver *ubwcp;
  1241. FENTRY();
  1242. if (!dmabuf) {
  1243. ERR("NULL dmabuf input ptr");
  1244. return -EINVAL;
  1245. }
  1246. if (!valid_dma_direction(dir)) {
  1247. ERR("invalid direction: %d", dir);
  1248. return -EINVAL;
  1249. }
  1250. buf = dma_buf_to_ubwcp_buf(dmabuf);
  1251. if (!buf) {
  1252. ERR("ubwcp_buf ptr not found");
  1253. return -1;
  1254. }
  1255. mutex_lock(&buf->lock);
  1256. if (!buf->buf_attr_set) {
  1257. ERR("lock() called on buffer, but attr not set");
  1258. goto err;
  1259. }
  1260. if (buf->buf_attr.image_format == UBWCP_LINEAR) {
  1261. ERR("lock() called on linear buffer");
  1262. goto err;
  1263. }
  1264. if (!buf->locked) {
  1265. DBG("first lock on buffer");
  1266. ubwcp = buf->ubwcp;
  1267. /* buf->desc could already be allocated because of perm range xlation */
  1268. if (!buf->desc) {
  1269. /* allocate a buffer descriptor */
  1270. buf->desc = ubwcp_buf_desc_allocate(buf->ubwcp);
  1271. if (!buf->desc) {
  1272. ERR("ubwcp_allocate_buf_desc() failed");
  1273. goto err;
  1274. }
  1275. memcpy(buf->desc->ptr, &buf->mmdata, sizeof(buf->mmdata));
  1276. /* Flushing of updated mmdata:
  1277. * mmdata is iocoherent and ubwcp will get it from CPU cache -
  1278. * *as long as* it has not cached that itself during previous
  1279. * access to the same descriptor.
  1280. *
  1281. * During unlock of previous use of this descriptor,
  1282. * we do hw flush, which will get rid of this mmdata from
  1283. * ubwcp cache.
  1284. *
  1285. * In addition, we also do a hw flush after enable_range_ck().
  1286. * That will also get rid of any speculative fetch of mmdata
  1287. * by the ubwcp hw. At this time, the assumption is that ubwcp
  1288. * will cache mmdata only for active descriptor. But if ubwcp
  1289. * is speculatively fetching mmdata for all descriptors
  1290. * (irrespetive of enabled or not), the flush during lock
  1291. * will be necessary to make sure ubwcp sees updated mmdata
  1292. * that we just updated
  1293. */
  1294. /* program ULA range for this buffer */
  1295. DBG("setting range check: descriptor_id: %d, addr: %p, size: %zx",
  1296. buf->desc->idx, buf->ula_pa, buf->ula_size);
  1297. ubwcp_hw_set_range_check(ubwcp->base, buf->desc->idx, buf->ula_pa,
  1298. buf->ula_size);
  1299. }
  1300. /* enable range check */
  1301. DBG("enabling range check, descriptor_id: %d", buf->desc->idx);
  1302. mutex_lock(&ubwcp->hw_range_ck_lock);
  1303. ubwcp_hw_enable_range_check(ubwcp->base, buf->desc->idx);
  1304. mutex_unlock(&ubwcp->hw_range_ck_lock);
  1305. /* Flush/invalidate UBWCP caches */
  1306. /* Why: cpu could have done a speculative fetch before
  1307. * enable_range_ck() and ubwcp in process of returning "default" data
  1308. * we don't want that stashing of default data pending.
  1309. * we force completion of that and then we also cpu invalidate which
  1310. * will get rid of that line.
  1311. */
  1312. ubwcp_flush(ubwcp);
  1313. /* Flush/invalidate ULA PA from CPU caches
  1314. * TBD: if (dir == READ or BIDIRECTION) //NOT for write
  1315. * -- Confirm with Chris if this can be skipped for write
  1316. */
  1317. dma_sync_single_for_cpu(ubwcp->dev, buf->ula_pa, buf->ula_size, dir);
  1318. buf->lock_dir = dir;
  1319. buf->locked = true;
  1320. } else {
  1321. DBG("buf already locked");
  1322. /* TBD: what if new buffer direction is not same as previous?
  1323. * must update the dir.
  1324. */
  1325. }
  1326. buf->lock_count++;
  1327. DBG("new lock_count: %d", buf->lock_count);
  1328. mutex_unlock(&buf->lock);
  1329. return ret;
  1330. err:
  1331. mutex_unlock(&buf->lock);
  1332. if (!ret)
  1333. ret = -1;
  1334. return ret;
  1335. }
  1336. /* This can be called as a result of external unlock() call or
  1337. * internally if free() is called without unlock().
  1338. * It can fail only for 1 reason: ubwcp_flush fails. currently we are ignoring the flush failure
  1339. * because it is hardware failure and no recovery path is defined.
  1340. */
  1341. static int unlock_internal(struct ubwcp_buf *buf, enum dma_data_direction dir, bool free_buffer)
  1342. {
  1343. struct ubwcp_driver *ubwcp;
  1344. DBG("current lock_count: %d", buf->lock_count);
  1345. if (free_buffer) {
  1346. buf->lock_count = 0;
  1347. DBG("Forced lock_count: %d", buf->lock_count);
  1348. } else {
  1349. buf->lock_count--;
  1350. DBG("new lock_count: %d", buf->lock_count);
  1351. if (buf->lock_count) {
  1352. DBG("more than 1 lock on buffer. waiting until last unlock");
  1353. return 0;
  1354. }
  1355. }
  1356. ubwcp = buf->ubwcp;
  1357. /* Flush/invalidate ULA PA from CPU caches */
  1358. //TBD: if (dir == WRITE or BIDIRECTION)
  1359. //dma_sync_single_for_device(ubwcp->dev, buf->ula_pa, buf->ula_size, dir);
  1360. /* TODO: Use flush work around, remove when no longer needed */
  1361. ubwcp_flush_cache_wa(ubwcp->dev, buf->ula_pa, buf->ula_size);
  1362. /* TBD: confirm with HW if this should be done before or
  1363. * after disable_range_ck()
  1364. */
  1365. ubwcp_flush(ubwcp);
  1366. /* disable range check */
  1367. DBG("disabling range check");
  1368. mutex_lock(&ubwcp->hw_range_ck_lock);
  1369. ubwcp_hw_disable_range_check(ubwcp->base, buf->desc->idx);
  1370. mutex_unlock(&ubwcp->hw_range_ck_lock);
  1371. /* release descriptor if perm range xlation is not set */
  1372. if (!buf->perm) {
  1373. ubwcp_buf_desc_free(buf->ubwcp, buf->desc);
  1374. buf->desc = NULL;
  1375. }
  1376. buf->locked = false;
  1377. return 0;
  1378. }
  1379. /**
  1380. * Unlock buffer from CPU access. This prepares ubwcp hw to
  1381. * safely allow for device access to the compressed buffer including any
  1382. * necessary cache maintenance ops. It may also free up certain ubwcp
  1383. * resources that could result in error when accessed by CPU in
  1384. * unlocked state.
  1385. *
  1386. * @param dmabuf : ptr to the dma buf
  1387. * @param direction : direction of access
  1388. *
  1389. * @return int : 0 on success, otherwise error code
  1390. */
  1391. static int ubwcp_unlock(struct dma_buf *dmabuf, enum dma_data_direction dir)
  1392. {
  1393. struct ubwcp_buf *buf;
  1394. int ret;
  1395. FENTRY();
  1396. if (!dmabuf) {
  1397. ERR("NULL dmabuf input ptr");
  1398. return -EINVAL;
  1399. }
  1400. if (!valid_dma_direction(dir)) {
  1401. ERR("invalid direction: %d", dir);
  1402. return -EINVAL;
  1403. }
  1404. buf = dma_buf_to_ubwcp_buf(dmabuf);
  1405. if (!buf) {
  1406. ERR("ubwcp_buf not found");
  1407. return -1;
  1408. }
  1409. if (!buf->locked) {
  1410. ERR("unlock() called on buffer which not in locked state");
  1411. return -1;
  1412. }
  1413. error_print_count = 0;
  1414. mutex_lock(&buf->lock);
  1415. ret = unlock_internal(buf, dir, false);
  1416. mutex_unlock(&buf->lock);
  1417. return ret;
  1418. }
  1419. /* Return buffer attributes for the given buffer */
  1420. int ubwcp_get_buf_attrs(struct dma_buf *dmabuf, struct ubwcp_buffer_attrs *attr)
  1421. {
  1422. int ret = 0;
  1423. struct ubwcp_buf *buf;
  1424. FENTRY();
  1425. if (!dmabuf) {
  1426. ERR("NULL dmabuf input ptr");
  1427. return -EINVAL;
  1428. }
  1429. if (!attr) {
  1430. ERR("NULL attr ptr");
  1431. return -EINVAL;
  1432. }
  1433. buf = dma_buf_to_ubwcp_buf(dmabuf);
  1434. if (!buf) {
  1435. ERR("ubwcp_buf ptr not found");
  1436. return -1;
  1437. }
  1438. mutex_lock(&buf->lock);
  1439. if (!buf->buf_attr_set) {
  1440. ERR("buffer attributes not set");
  1441. mutex_unlock(&buf->lock);
  1442. return -1;
  1443. }
  1444. *attr = buf->buf_attr;
  1445. mutex_unlock(&buf->lock);
  1446. return ret;
  1447. }
  1448. EXPORT_SYMBOL(ubwcp_get_buf_attrs);
  1449. /* Set permanent range translation.
  1450. * enable: Descriptor will be reserved for this buffer until disabled,
  1451. * making lock/unlock quicker.
  1452. * disable: Descriptor will not be reserved for this buffer. Instead,
  1453. * descriptor will be allocated and released for each lock/unlock.
  1454. * If currently allocated but not being used, descriptor will be
  1455. * released.
  1456. */
  1457. int ubwcp_set_perm_range_translation(struct dma_buf *dmabuf, bool enable)
  1458. {
  1459. int ret = 0;
  1460. struct ubwcp_buf *buf;
  1461. FENTRY();
  1462. if (!dmabuf) {
  1463. ERR("NULL dmabuf input ptr");
  1464. return -EINVAL;
  1465. }
  1466. buf = dma_buf_to_ubwcp_buf(dmabuf);
  1467. if (!buf) {
  1468. ERR("ubwcp_buf not found");
  1469. return -1;
  1470. }
  1471. /* not implemented */
  1472. if (1) {
  1473. ERR("API not implemented yet");
  1474. return -1;
  1475. }
  1476. /* TBD: make sure we acquire buf lock while setting this so there is
  1477. * no race condition with attr_set/lock/unlock
  1478. */
  1479. buf->perm = enable;
  1480. /* if "disable" and we have allocated a desc and it is not being
  1481. * used currently, release it
  1482. */
  1483. if (!enable && buf->desc && !buf->locked) {
  1484. ubwcp_buf_desc_free(buf->ubwcp, buf->desc);
  1485. buf->desc = NULL;
  1486. /* Flush/invalidate UBWCP caches */
  1487. //TBD: need to do anything?
  1488. }
  1489. return ret;
  1490. }
  1491. EXPORT_SYMBOL(ubwcp_set_perm_range_translation);
  1492. /**
  1493. * Free up ubwcp resources for this buffer.
  1494. *
  1495. * @param dmabuf : ptr to the dma buf
  1496. *
  1497. * @return int : 0 on success, otherwise error code
  1498. */
  1499. static int ubwcp_free_buffer(struct dma_buf *dmabuf)
  1500. {
  1501. int ret = 0;
  1502. struct ubwcp_buf *buf;
  1503. struct ubwcp_driver *ubwcp;
  1504. FENTRY();
  1505. if (!dmabuf) {
  1506. ERR("NULL dmabuf input ptr");
  1507. return -EINVAL;
  1508. }
  1509. buf = dma_buf_to_ubwcp_buf(dmabuf);
  1510. if (!buf) {
  1511. ERR("ubwcp_buf ptr not found");
  1512. return -1;
  1513. }
  1514. mutex_lock(&buf->lock);
  1515. ubwcp = buf->ubwcp;
  1516. if (buf->locked) {
  1517. DBG("free() called without unlock. unlock()'ing first...");
  1518. ret = unlock_internal(buf, buf->lock_dir, true);
  1519. if (ret)
  1520. ERR("unlock_internal(): failed : %d, but continuing free()", ret);
  1521. }
  1522. /* if we are still holding a desc, release it. this can happen only if perm == true */
  1523. if (buf->desc) {
  1524. WARN_ON(!buf->perm); /* TBD: change to BUG() later...*/
  1525. ubwcp_buf_desc_free(buf->ubwcp, buf->desc);
  1526. buf->desc = NULL;
  1527. }
  1528. if (buf->buf_attr_set)
  1529. reset_buf_attrs(buf);
  1530. mutex_lock(&ubwcp->buf_table_lock);
  1531. hash_del(&buf->hnode);
  1532. kfree(buf);
  1533. /* If this is the last buffer being freed, power off ubwcp */
  1534. if (hash_empty(ubwcp->buf_table)) {
  1535. DBG("last buffer: ~~~~~~~~~~~");
  1536. /* TBD: If everything is working fine, ubwcp_flush() should not
  1537. * be needed here. Each buffer free logic should be taking
  1538. * care of flush. Just a note for now. Might need to add the
  1539. * flush here for debug purpose.
  1540. */
  1541. DBG("Calling remove_memory() for ULA PA pool");
  1542. ret = remove_memory(ubwcp->ula_pool_base, ubwcp->ula_pool_size);
  1543. if (ret) {
  1544. ERR("remove_memory failed st:0x%lx sz:0x%lx err: %d",
  1545. ubwcp->ula_pool_base,
  1546. ubwcp->ula_pool_size, ret);
  1547. goto err_remove_mem;
  1548. } else {
  1549. DBG("DONE: calling remove_memory() for ULA PA pool");
  1550. }
  1551. DBG("Calling power OFF ...");
  1552. ubwcp_power(ubwcp, false);
  1553. }
  1554. mutex_unlock(&ubwcp->buf_table_lock);
  1555. return ret;
  1556. err_remove_mem:
  1557. mutex_unlock(&ubwcp->buf_table_lock);
  1558. if (!ret)
  1559. ret = -1;
  1560. DBG("returning error: %d", ret);
  1561. return ret;
  1562. }
  1563. /* file open: TBD: increment ref count? */
  1564. static int ubwcp_open(struct inode *i, struct file *f)
  1565. {
  1566. return 0;
  1567. }
  1568. /* file open: TBD: decrement ref count? */
  1569. static int ubwcp_close(struct inode *i, struct file *f)
  1570. {
  1571. return 0;
  1572. }
  1573. /* handle IOCTLs */
  1574. static long ubwcp_ioctl(struct file *file, unsigned int ioctl_num, unsigned long ioctl_param)
  1575. {
  1576. struct ubwcp_ioctl_buffer_attrs buf_attr_ioctl;
  1577. struct ubwcp_ioctl_hw_version hw_ver;
  1578. switch (ioctl_num) {
  1579. case UBWCP_IOCTL_SET_BUF_ATTR:
  1580. if (copy_from_user(&buf_attr_ioctl, (const void __user *) ioctl_param,
  1581. sizeof(buf_attr_ioctl))) {
  1582. ERR("ERROR: copy_from_user() failed");
  1583. return -EFAULT;
  1584. }
  1585. DBG("IOCTL : SET_BUF_ATTR: fd = %d", buf_attr_ioctl.fd);
  1586. return ubwcp_set_buf_attrs_ioctl(&buf_attr_ioctl);
  1587. case UBWCP_IOCTL_GET_HW_VER:
  1588. DBG("IOCTL : GET_HW_VER");
  1589. ubwcp_get_hw_version(&hw_ver);
  1590. if (copy_to_user((void __user *)ioctl_param, &hw_ver, sizeof(hw_ver))) {
  1591. ERR("ERROR: copy_to_user() failed");
  1592. return -EFAULT;
  1593. }
  1594. break;
  1595. default:
  1596. ERR("Invalid ioctl_num = %d", ioctl_num);
  1597. return -EINVAL;
  1598. }
  1599. return 0;
  1600. }
  1601. static const struct file_operations ubwcp_fops = {
  1602. .owner = THIS_MODULE,
  1603. .open = ubwcp_open,
  1604. .release = ubwcp_close,
  1605. .unlocked_ioctl = ubwcp_ioctl,
  1606. };
  1607. static int ubwcp_debugfs_init(struct ubwcp_driver *ubwcp)
  1608. {
  1609. struct dentry *debugfs_root;
  1610. debugfs_root = debugfs_create_dir("ubwcp", NULL);
  1611. if (!debugfs_root) {
  1612. pr_warn("Failed to create debugfs for ubwcp\n");
  1613. return -1;
  1614. }
  1615. debugfs_create_u32("debug_trace_enable", 0644, debugfs_root, &ubwcp_debug_trace_enable);
  1616. ubwcp->debugfs_root = debugfs_root;
  1617. return 0;
  1618. }
  1619. static void ubwcp_debugfs_deinit(struct ubwcp_driver *ubwcp)
  1620. {
  1621. debugfs_remove_recursive(ubwcp->debugfs_root);
  1622. }
  1623. /* ubwcp char device initialization */
  1624. static int ubwcp_cdev_init(struct ubwcp_driver *ubwcp)
  1625. {
  1626. int ret;
  1627. dev_t devt;
  1628. struct class *dev_class;
  1629. struct device *dev_sys;
  1630. /* allocate major device number (/proc/devices -> major_num ubwcp) */
  1631. ret = alloc_chrdev_region(&devt, 0, UBWCP_NUM_DEVICES, UBWCP_DEVICE_NAME);
  1632. if (ret) {
  1633. ERR("alloc_chrdev_region() failed: %d", ret);
  1634. return ret;
  1635. }
  1636. /* create device class (/sys/class/ubwcp_class) */
  1637. dev_class = class_create(THIS_MODULE, "ubwcp_class");
  1638. if (IS_ERR(dev_class)) {
  1639. ERR("class_create() failed");
  1640. return -1;
  1641. }
  1642. /* Create device and register with sysfs
  1643. * (/sys/class/ubwcp_class/ubwcp/... -> dev/power/subsystem/uevent)
  1644. */
  1645. dev_sys = device_create(dev_class, NULL, devt, NULL,
  1646. UBWCP_DEVICE_NAME);
  1647. if (IS_ERR(dev_sys)) {
  1648. ERR("device_create() failed");
  1649. return -1;
  1650. }
  1651. /* register file operations and get cdev */
  1652. cdev_init(&ubwcp->cdev, &ubwcp_fops);
  1653. /* associate cdev and device major/minor with file system
  1654. * can do file ops on /dev/ubwcp after this
  1655. */
  1656. ret = cdev_add(&ubwcp->cdev, devt, 1);
  1657. if (ret) {
  1658. ERR("cdev_add() failed");
  1659. return -1;
  1660. }
  1661. ubwcp->devt = devt;
  1662. ubwcp->dev_class = dev_class;
  1663. ubwcp->dev_sys = dev_sys;
  1664. return 0;
  1665. }
  1666. static void ubwcp_cdev_deinit(struct ubwcp_driver *ubwcp)
  1667. {
  1668. device_destroy(ubwcp->dev_class, ubwcp->devt);
  1669. class_destroy(ubwcp->dev_class);
  1670. cdev_del(&ubwcp->cdev);
  1671. unregister_chrdev_region(ubwcp->devt, UBWCP_NUM_DEVICES);
  1672. }
  1673. #define ERR_PRINT_COUNT_MAX 21
  1674. /* TBD: use proper rate limit for debug prints */
  1675. irqreturn_t ubwcp_irq_handler(int irq, void *ptr)
  1676. {
  1677. struct ubwcp_driver *ubwcp;
  1678. void __iomem *base;
  1679. u64 src;
  1680. error_print_count++;
  1681. ubwcp = (struct ubwcp_driver *) ptr;
  1682. base = ubwcp->base;
  1683. if (irq == ubwcp->irq_range_ck_rd) {
  1684. if (error_print_count < ERR_PRINT_COUNT_MAX) {
  1685. src = ubwcp_hw_interrupt_src_address(base, 0);
  1686. ERR("check range read error: src: 0x%llx", src << 6);
  1687. }
  1688. ubwcp_hw_interrupt_clear(ubwcp->base, 0);
  1689. } else if (irq == ubwcp->irq_range_ck_wr) {
  1690. if (error_print_count < ERR_PRINT_COUNT_MAX) {
  1691. src = ubwcp_hw_interrupt_src_address(base, 1);
  1692. ERR("check range write error: src: 0x%llx", src << 6);
  1693. }
  1694. ubwcp_hw_interrupt_clear(ubwcp->base, 1);
  1695. } else if (irq == ubwcp->irq_encode) {
  1696. if (error_print_count < ERR_PRINT_COUNT_MAX) {
  1697. src = ubwcp_hw_interrupt_src_address(base, 3);
  1698. ERR("encode error: src: 0x%llx", src << 6);
  1699. }
  1700. ubwcp_hw_interrupt_clear(ubwcp->base, 3); //TBD: encode is bit-3 instead of bit-2
  1701. } else if (irq == ubwcp->irq_decode) {
  1702. if (error_print_count < ERR_PRINT_COUNT_MAX) {
  1703. src = ubwcp_hw_interrupt_src_address(base, 2);
  1704. ERR("decode error: src: 0x%llx", src << 6);
  1705. }
  1706. ubwcp_hw_interrupt_clear(ubwcp->base, 2); //TBD: decode is bit-2 instead of bit-3
  1707. } else {
  1708. ERR("unknown irq: %d", irq);
  1709. return IRQ_NONE;
  1710. }
  1711. return IRQ_HANDLED;
  1712. }
  1713. static int ubwcp_interrupt_register(struct platform_device *pdev, struct ubwcp_driver *ubwcp)
  1714. {
  1715. int ret = 0;
  1716. struct device *dev = &pdev->dev;
  1717. FENTRY();
  1718. ubwcp->irq_range_ck_rd = platform_get_irq(pdev, 0);
  1719. if (ubwcp->irq_range_ck_rd < 0)
  1720. return ubwcp->irq_range_ck_rd;
  1721. ubwcp->irq_range_ck_wr = platform_get_irq(pdev, 1);
  1722. if (ubwcp->irq_range_ck_wr < 0)
  1723. return ubwcp->irq_range_ck_wr;
  1724. ubwcp->irq_encode = platform_get_irq(pdev, 2);
  1725. if (ubwcp->irq_encode < 0)
  1726. return ubwcp->irq_encode;
  1727. ubwcp->irq_decode = platform_get_irq(pdev, 3);
  1728. if (ubwcp->irq_decode < 0)
  1729. return ubwcp->irq_decode;
  1730. DBG("got irqs: %d %d %d %d", ubwcp->irq_range_ck_rd,
  1731. ubwcp->irq_range_ck_wr,
  1732. ubwcp->irq_encode,
  1733. ubwcp->irq_decode);
  1734. ret = devm_request_irq(dev, ubwcp->irq_range_ck_rd, ubwcp_irq_handler, 0, "ubwcp", ubwcp);
  1735. if (ret) {
  1736. ERR("request_irq() failed. irq: %d ret: %d",
  1737. ubwcp->irq_range_ck_rd, ret);
  1738. return ret;
  1739. }
  1740. ret = devm_request_irq(dev, ubwcp->irq_range_ck_wr, ubwcp_irq_handler, 0, "ubwcp", ubwcp);
  1741. if (ret) {
  1742. ERR("request_irq() failed. irq: %d ret: %d",
  1743. ubwcp->irq_range_ck_wr, ret);
  1744. return ret;
  1745. }
  1746. ret = devm_request_irq(dev, ubwcp->irq_encode, ubwcp_irq_handler, 0, "ubwcp", ubwcp);
  1747. if (ret) {
  1748. ERR("request_irq() failed. irq: %d ret: %d",
  1749. ubwcp->irq_encode, ret);
  1750. return ret;
  1751. }
  1752. ret = devm_request_irq(dev, ubwcp->irq_decode, ubwcp_irq_handler, 0, "ubwcp", ubwcp);
  1753. if (ret) {
  1754. ERR("request_irq() failed. irq: %d ret: %d",
  1755. ubwcp->irq_decode, ret);
  1756. return ret;
  1757. }
  1758. return ret;
  1759. }
  1760. /* ubwcp device probe */
  1761. static int qcom_ubwcp_probe(struct platform_device *pdev)
  1762. {
  1763. int ret = 0;
  1764. struct ubwcp_driver *ubwcp;
  1765. struct device *ubwcp_dev = &pdev->dev;
  1766. FENTRY();
  1767. ubwcp = devm_kzalloc(ubwcp_dev, sizeof(*ubwcp), GFP_KERNEL);
  1768. if (!ubwcp) {
  1769. ERR("devm_kzalloc() failed");
  1770. return -ENOMEM;
  1771. }
  1772. ubwcp->dev = &pdev->dev;
  1773. ret = dma_set_mask_and_coherent(ubwcp->dev, DMA_BIT_MASK(64));
  1774. #ifdef UBWCP_USE_SMC
  1775. {
  1776. struct resource res;
  1777. of_address_to_resource(ubwcp_dev->of_node, 0, &res);
  1778. ubwcp->base = (void __iomem *) res.start;
  1779. DBG("Using SMC calls. base: %p", ubwcp->base);
  1780. }
  1781. #else
  1782. ubwcp->base = devm_platform_ioremap_resource(pdev, 0);
  1783. if (IS_ERR(ubwcp->base)) {
  1784. ERR("devm ioremap() failed: %d", PTR_ERR(ubwcp->base));
  1785. return PTR_ERR(ubwcp->base);
  1786. }
  1787. DBG("ubwcp->base: %p", ubwcp->base);
  1788. #endif
  1789. ret = of_property_read_u64_index(ubwcp_dev->of_node, "ula_range", 0, &ubwcp->ula_pool_base);
  1790. if (ret) {
  1791. ERR("failed reading ula_range (base): %d", ret);
  1792. return ret;
  1793. }
  1794. DBG("ubwcp: ula_range: base = 0x%lx", ubwcp->ula_pool_base);
  1795. ret = of_property_read_u64_index(ubwcp_dev->of_node, "ula_range", 1, &ubwcp->ula_pool_size);
  1796. if (ret) {
  1797. ERR("failed reading ula_range (size): %d", ret);
  1798. return ret;
  1799. }
  1800. DBG("ubwcp: ula_range: size = 0x%lx", ubwcp->ula_pool_size);
  1801. /*TBD: remove later. reducing size for quick testing...*/
  1802. ubwcp->ula_pool_size = 0x20000000; //500MB instead of 8GB
  1803. if (ubwcp_interrupt_register(pdev, ubwcp))
  1804. return -1;
  1805. /* Regulator */
  1806. ubwcp->vdd = devm_regulator_get(ubwcp_dev, "vdd");
  1807. if (IS_ERR(ubwcp->vdd)) {
  1808. ret = PTR_ERR(ubwcp->vdd);
  1809. ERR("devm_regulator_get() failed: %d", ret);
  1810. return ret;
  1811. }
  1812. mutex_init(&ubwcp->desc_lock);
  1813. mutex_init(&ubwcp->buf_table_lock);
  1814. mutex_init(&ubwcp->ula_lock);
  1815. mutex_init(&ubwcp->ubwcp_flush_lock);
  1816. mutex_init(&ubwcp->hw_range_ck_lock);
  1817. if (ubwcp_power(ubwcp, true))
  1818. return -1;
  1819. if (ubwcp_cdev_init(ubwcp))
  1820. return -1;
  1821. if (ubwcp_debugfs_init(ubwcp))
  1822. return -1;
  1823. /* create ULA pool */
  1824. ubwcp->ula_pool = gen_pool_create(12, -1);
  1825. if (!ubwcp->ula_pool) {
  1826. ERR("failed gen_pool_create()");
  1827. ret = -1;
  1828. goto err_pool_create;
  1829. }
  1830. ret = gen_pool_add(ubwcp->ula_pool, ubwcp->ula_pool_base, ubwcp->ula_pool_size, -1);
  1831. if (ret) {
  1832. ERR("failed gen_pool_add(): %d", ret);
  1833. ret = -1;
  1834. goto err_pool_add;
  1835. }
  1836. /* register the default config mmap function. */
  1837. ubwcp->mmap_config_fptr = msm_ubwcp_dma_buf_configure_mmap;
  1838. hash_init(ubwcp->buf_table);
  1839. ubwcp_buf_desc_list_init(ubwcp);
  1840. image_format_init(ubwcp);
  1841. /* one time hw init */
  1842. ubwcp_hw_one_time_init(ubwcp->base);
  1843. ubwcp_hw_version(ubwcp->base, &ubwcp->hw_ver_major, &ubwcp->hw_ver_minor);
  1844. DBG("read version: major %d, minor %d",
  1845. ubwcp->hw_ver_major, ubwcp->hw_ver_minor);
  1846. if (ubwcp->hw_ver_major == 0) {
  1847. ERR("Failed to read HW version");
  1848. ret = -1;
  1849. goto err_pool_add;
  1850. }
  1851. /* set pdev->dev->driver_data = ubwcp */
  1852. platform_set_drvdata(pdev, ubwcp);
  1853. me = ubwcp;
  1854. /* enable all 4 interrupts */
  1855. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_READ_ERROR, true);
  1856. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_WRITE_ERROR, true);
  1857. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_ENCODE_ERROR, true);
  1858. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_DECODE_ERROR, true);
  1859. /* Turn OFF until buffers are allocated */
  1860. if (ubwcp_power(ubwcp, false)) {
  1861. ret = -1;
  1862. goto err_power_off;
  1863. }
  1864. ret = msm_ubwcp_set_ops(ubwcp_init_buffer, ubwcp_free_buffer, ubwcp_lock, ubwcp_unlock);
  1865. if (ret) {
  1866. ERR("msm_ubwcp_set_ops() failed: %d", ret);
  1867. goto err_power_off;
  1868. } else {
  1869. DBG("msm_ubwcp_set_ops(): success"); }
  1870. return ret;
  1871. err_power_off:
  1872. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_READ_ERROR, false);
  1873. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_WRITE_ERROR, false);
  1874. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_ENCODE_ERROR, false);
  1875. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_DECODE_ERROR, false);
  1876. err_pool_add:
  1877. gen_pool_destroy(ubwcp->ula_pool);
  1878. err_pool_create:
  1879. ubwcp_cdev_deinit(ubwcp);
  1880. return ret;
  1881. }
  1882. /* buffer context bank device probe */
  1883. static int ubwcp_probe_cb_buf(struct platform_device *pdev)
  1884. {
  1885. struct ubwcp_driver *ubwcp;
  1886. FENTRY();
  1887. ubwcp = dev_get_drvdata(pdev->dev.parent);
  1888. if (!ubwcp) {
  1889. ERR("failed to get ubwcp ptr");
  1890. return -EINVAL;
  1891. }
  1892. /* save the buffer cb device */
  1893. ubwcp->dev_buf_cb = &pdev->dev;
  1894. return 0;
  1895. }
  1896. /* descriptor context bank device probe */
  1897. static int ubwcp_probe_cb_desc(struct platform_device *pdev)
  1898. {
  1899. int ret = 0;
  1900. struct ubwcp_driver *ubwcp;
  1901. FENTRY();
  1902. ubwcp = dev_get_drvdata(pdev->dev.parent);
  1903. if (!ubwcp) {
  1904. ERR("failed to get ubwcp ptr");
  1905. return -EINVAL;
  1906. }
  1907. ubwcp->buffer_desc_size = UBWCP_BUFFER_DESC_OFFSET *
  1908. UBWCP_BUFFER_DESC_COUNT;
  1909. ubwcp->dev_desc_cb = &pdev->dev;
  1910. dma_set_max_seg_size(ubwcp->dev_desc_cb, DMA_BIT_MASK(32));
  1911. dma_set_seg_boundary(ubwcp->dev_desc_cb, (unsigned long)DMA_BIT_MASK(64));
  1912. /* Allocate buffer descriptors. UBWCP is iocoherent device.
  1913. * Thus we don't need to flush after updates to buffer descriptors.
  1914. */
  1915. ubwcp->buffer_desc_base = dma_alloc_coherent(ubwcp->dev_desc_cb,
  1916. ubwcp->buffer_desc_size,
  1917. &ubwcp->buffer_desc_dma_handle,
  1918. GFP_KERNEL);
  1919. if (!ubwcp->buffer_desc_base) {
  1920. ERR("failed to allocate desc buffer");
  1921. return -ENOMEM;
  1922. }
  1923. DBG("desc_base = %p size = %zu", ubwcp->buffer_desc_base,
  1924. ubwcp->buffer_desc_size);
  1925. //TBD:
  1926. ubwcp_power(ubwcp, true);
  1927. ubwcp_hw_set_buf_desc(ubwcp->base, (u64) ubwcp->buffer_desc_dma_handle,
  1928. UBWCP_BUFFER_DESC_OFFSET);
  1929. ubwcp_power(ubwcp, false);
  1930. return ret;
  1931. }
  1932. /* buffer context bank device remove */
  1933. static int ubwcp_remove_cb_buf(struct platform_device *pdev)
  1934. {
  1935. struct ubwcp_driver *ubwcp;
  1936. FENTRY();
  1937. ubwcp = dev_get_drvdata(pdev->dev.parent);
  1938. if (!ubwcp) {
  1939. ERR("failed to get ubwcp ptr");
  1940. return -EINVAL;
  1941. }
  1942. /* remove buf_cb reference */
  1943. ubwcp->dev_buf_cb = NULL;
  1944. return 0;
  1945. }
  1946. /* descriptor context bank device remove */
  1947. static int ubwcp_remove_cb_desc(struct platform_device *pdev)
  1948. {
  1949. struct ubwcp_driver *ubwcp;
  1950. FENTRY();
  1951. ubwcp = dev_get_drvdata(pdev->dev.parent);
  1952. if (!ubwcp) {
  1953. ERR("failed to get ubwcp ptr");
  1954. return -EINVAL;
  1955. }
  1956. if (!ubwcp->dev_desc_cb) {
  1957. ERR("ubwcp->dev_desc_cb == NULL");
  1958. return -1;
  1959. }
  1960. ubwcp_power(ubwcp, true);
  1961. ubwcp_hw_set_buf_desc(ubwcp->base, 0x0, 0x0);
  1962. ubwcp_power(ubwcp, false);
  1963. dma_free_coherent(ubwcp->dev_desc_cb,
  1964. ubwcp->buffer_desc_size,
  1965. ubwcp->buffer_desc_base,
  1966. ubwcp->buffer_desc_dma_handle);
  1967. ubwcp->buffer_desc_base = NULL;
  1968. ubwcp->buffer_desc_dma_handle = 0;
  1969. return 0;
  1970. }
  1971. /* ubwcp device remove */
  1972. static int qcom_ubwcp_remove(struct platform_device *pdev)
  1973. {
  1974. size_t avail;
  1975. size_t psize;
  1976. struct ubwcp_driver *ubwcp;
  1977. FENTRY();
  1978. /* get pdev->dev->driver_data = ubwcp */
  1979. ubwcp = platform_get_drvdata(pdev);
  1980. if (!ubwcp) {
  1981. ERR("ubwcp == NULL");
  1982. return -1;
  1983. }
  1984. ubwcp_power(ubwcp, true);
  1985. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_READ_ERROR, false);
  1986. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_WRITE_ERROR, false);
  1987. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_ENCODE_ERROR, false);
  1988. ubwcp_hw_interrupt_enable(ubwcp->base, INTERRUPT_DECODE_ERROR, false);
  1989. ubwcp_power(ubwcp, false);
  1990. /* before destroying, make sure pool is empty. otherwise pool_destroy() panics.
  1991. * TBD: remove this check for production code and let it panic
  1992. */
  1993. avail = gen_pool_avail(ubwcp->ula_pool);
  1994. psize = gen_pool_size(ubwcp->ula_pool);
  1995. if (psize != avail) {
  1996. ERR("gen_pool is not empty! avail: %zx size: %zx", avail, psize);
  1997. ERR("skipping pool destroy....cause it will PANIC. Fix this!!!!");
  1998. WARN(1, "Fix this!");
  1999. } else {
  2000. gen_pool_destroy(ubwcp->ula_pool);
  2001. }
  2002. ubwcp_debugfs_deinit(ubwcp);
  2003. ubwcp_cdev_deinit(ubwcp);
  2004. return 0;
  2005. }
  2006. /* top level ubwcp device probe function */
  2007. static int ubwcp_probe(struct platform_device *pdev)
  2008. {
  2009. const char *compatible = "";
  2010. FENTRY();
  2011. if (of_device_is_compatible(pdev->dev.of_node, "qcom,ubwcp"))
  2012. return qcom_ubwcp_probe(pdev);
  2013. else if (of_device_is_compatible(pdev->dev.of_node, "qcom,ubwcp-context-bank-desc"))
  2014. return ubwcp_probe_cb_desc(pdev);
  2015. else if (of_device_is_compatible(pdev->dev.of_node, "qcom,ubwcp-context-bank-buf"))
  2016. return ubwcp_probe_cb_buf(pdev);
  2017. of_property_read_string(pdev->dev.of_node, "compatible", &compatible);
  2018. ERR("unknown device: %s", compatible);
  2019. WARN_ON(1);
  2020. return -EINVAL;
  2021. }
  2022. /* top level ubwcp device remove function */
  2023. static int ubwcp_remove(struct platform_device *pdev)
  2024. {
  2025. const char *compatible = "";
  2026. FENTRY();
  2027. /* TBD: what if buffers are still allocated? locked? etc.
  2028. * also should turn off power?
  2029. */
  2030. if (of_device_is_compatible(pdev->dev.of_node, "qcom,ubwcp"))
  2031. return qcom_ubwcp_remove(pdev);
  2032. else if (of_device_is_compatible(pdev->dev.of_node, "qcom,ubwcp-context-bank-desc"))
  2033. return ubwcp_remove_cb_desc(pdev);
  2034. else if (of_device_is_compatible(pdev->dev.of_node, "qcom,ubwcp-context-bank-buf"))
  2035. return ubwcp_remove_cb_buf(pdev);
  2036. of_property_read_string(pdev->dev.of_node, "compatible", &compatible);
  2037. ERR("unknown device: %s", compatible);
  2038. WARN_ON(1);
  2039. return -EINVAL;
  2040. }
  2041. static const struct of_device_id ubwcp_dt_match[] = {
  2042. {.compatible = "qcom,ubwcp"},
  2043. {.compatible = "qcom,ubwcp-context-bank-desc"},
  2044. {.compatible = "qcom,ubwcp-context-bank-buf"},
  2045. {}
  2046. };
  2047. struct platform_driver ubwcp_platform_driver = {
  2048. .probe = ubwcp_probe,
  2049. .remove = ubwcp_remove,
  2050. .driver = {
  2051. .name = "qcom,ubwcp",
  2052. .of_match_table = ubwcp_dt_match,
  2053. },
  2054. };
  2055. int ubwcp_init(void)
  2056. {
  2057. int ret = 0;
  2058. DBG("+++++++++++");
  2059. ret = platform_driver_register(&ubwcp_platform_driver);
  2060. if (ret)
  2061. ERR("platform_driver_register() failed: %d", ret);
  2062. return ret;
  2063. }
  2064. void ubwcp_exit(void)
  2065. {
  2066. platform_driver_unregister(&ubwcp_platform_driver);
  2067. DBG("-----------");
  2068. }
  2069. module_init(ubwcp_init);
  2070. module_exit(ubwcp_exit);
  2071. MODULE_LICENSE("GPL");