intel_bufmgr_gem.c 101 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744
  1. /**************************************************************************
  2. *
  3. * Copyright © 2007 Red Hat Inc.
  4. * Copyright © 2007-2012 Intel Corporation
  5. * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
  6. * All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the
  10. * "Software"), to deal in the Software without restriction, including
  11. * without limitation the rights to use, copy, modify, merge, publish,
  12. * distribute, sub license, and/or sell copies of the Software, and to
  13. * permit persons to whom the Software is furnished to do so, subject to
  14. * the following conditions:
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  20. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  21. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  22. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * The above copyright notice and this permission notice (including the
  25. * next paragraph) shall be included in all copies or substantial portions
  26. * of the Software.
  27. *
  28. *
  29. **************************************************************************/
  30. /*
  31. * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
  32. * Keith Whitwell <keithw-at-tungstengraphics-dot-com>
  33. * Eric Anholt <eric@anholt.net>
  34. * Dave Airlie <airlied@linux.ie>
  35. */
  36. #include <xf86drm.h>
  37. #include <xf86atomic.h>
  38. #include <fcntl.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include <assert.h>
  44. #include <pthread.h>
  45. #include <sys/ioctl.h>
  46. #include <sys/stat.h>
  47. #include <sys/types.h>
  48. #include <stdbool.h>
  49. #include "errno.h"
  50. #ifndef ETIME
  51. #define ETIME ETIMEDOUT
  52. #endif
  53. #include "libdrm_macros.h"
  54. #include "libdrm_lists.h"
  55. #include "intel_bufmgr.h"
  56. #include "intel_bufmgr_priv.h"
  57. #include "intel_chipset.h"
  58. #include "string.h"
  59. #include "i915_drm.h"
  60. #include "uthash.h"
  61. #if HAVE_VALGRIND
  62. #include <valgrind.h>
  63. #include <memcheck.h>
  64. #define VG(x) x
  65. #else
  66. #define VG(x)
  67. #endif
  68. #define memclear(s) memset(&s, 0, sizeof(s))
  69. #define DBG(...) do { \
  70. if (bufmgr_gem->bufmgr.debug) \
  71. fprintf(stderr, __VA_ARGS__); \
  72. } while (0)
  73. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  74. #define MAX2(A, B) ((A) > (B) ? (A) : (B))
  75. /**
  76. * upper_32_bits - return bits 32-63 of a number
  77. * @n: the number we're accessing
  78. *
  79. * A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
  80. * the "right shift count >= width of type" warning when that quantity is
  81. * 32-bits.
  82. */
  83. #define upper_32_bits(n) ((__u32)(((n) >> 16) >> 16))
  84. /**
  85. * lower_32_bits - return bits 0-31 of a number
  86. * @n: the number we're accessing
  87. */
  88. #define lower_32_bits(n) ((__u32)(n))
  89. typedef struct _drm_intel_bo_gem drm_intel_bo_gem;
  90. struct drm_intel_gem_bo_bucket {
  91. drmMMListHead head;
  92. unsigned long size;
  93. };
  94. typedef struct _drm_intel_bufmgr_gem {
  95. drm_intel_bufmgr bufmgr;
  96. atomic_t refcount;
  97. int fd;
  98. int max_relocs;
  99. pthread_mutex_t lock;
  100. struct drm_i915_gem_exec_object2 *exec2_objects;
  101. drm_intel_bo **exec_bos;
  102. int exec_size;
  103. int exec_count;
  104. /** Array of lists of cached gem objects of power-of-two sizes */
  105. struct drm_intel_gem_bo_bucket cache_bucket[14 * 4];
  106. int num_buckets;
  107. time_t time;
  108. drmMMListHead managers;
  109. drm_intel_bo_gem *name_table;
  110. drm_intel_bo_gem *handle_table;
  111. drmMMListHead vma_cache;
  112. int vma_count, vma_open, vma_max;
  113. uint64_t gtt_size;
  114. int available_fences;
  115. int pci_device;
  116. int gen;
  117. unsigned int has_bsd : 1;
  118. unsigned int has_blt : 1;
  119. unsigned int has_relaxed_fencing : 1;
  120. unsigned int has_llc : 1;
  121. unsigned int has_wait_timeout : 1;
  122. unsigned int bo_reuse : 1;
  123. unsigned int no_exec : 1;
  124. unsigned int has_vebox : 1;
  125. unsigned int has_exec_async : 1;
  126. bool fenced_relocs;
  127. struct {
  128. void *ptr;
  129. uint32_t handle;
  130. } userptr_active;
  131. } drm_intel_bufmgr_gem;
  132. #define DRM_INTEL_RELOC_FENCE (1<<0)
  133. typedef struct _drm_intel_reloc_target_info {
  134. drm_intel_bo *bo;
  135. int flags;
  136. } drm_intel_reloc_target;
  137. struct _drm_intel_bo_gem {
  138. drm_intel_bo bo;
  139. atomic_t refcount;
  140. uint32_t gem_handle;
  141. const char *name;
  142. /**
  143. * Kenel-assigned global name for this object
  144. *
  145. * List contains both flink named and prime fd'd objects
  146. */
  147. unsigned int global_name;
  148. UT_hash_handle handle_hh;
  149. UT_hash_handle name_hh;
  150. /**
  151. * Index of the buffer within the validation list while preparing a
  152. * batchbuffer execution.
  153. */
  154. int validate_index;
  155. /**
  156. * Current tiling mode
  157. */
  158. uint32_t tiling_mode;
  159. uint32_t swizzle_mode;
  160. unsigned long stride;
  161. unsigned long kflags;
  162. time_t free_time;
  163. /** Array passed to the DRM containing relocation information. */
  164. struct drm_i915_gem_relocation_entry *relocs;
  165. /**
  166. * Array of info structs corresponding to relocs[i].target_handle etc
  167. */
  168. drm_intel_reloc_target *reloc_target_info;
  169. /** Number of entries in relocs */
  170. int reloc_count;
  171. /** Array of BOs that are referenced by this buffer and will be softpinned */
  172. drm_intel_bo **softpin_target;
  173. /** Number softpinned BOs that are referenced by this buffer */
  174. int softpin_target_count;
  175. /** Maximum amount of softpinned BOs that are referenced by this buffer */
  176. int softpin_target_size;
  177. /** Mapped address for the buffer, saved across map/unmap cycles */
  178. void *mem_virtual;
  179. /** GTT virtual address for the buffer, saved across map/unmap cycles */
  180. void *gtt_virtual;
  181. /** WC CPU address for the buffer, saved across map/unmap cycles */
  182. void *wc_virtual;
  183. /**
  184. * Virtual address of the buffer allocated by user, used for userptr
  185. * objects only.
  186. */
  187. void *user_virtual;
  188. int map_count;
  189. drmMMListHead vma_list;
  190. /** BO cache list */
  191. drmMMListHead head;
  192. /**
  193. * Boolean of whether this BO and its children have been included in
  194. * the current drm_intel_bufmgr_check_aperture_space() total.
  195. */
  196. bool included_in_check_aperture;
  197. /**
  198. * Boolean of whether this buffer has been used as a relocation
  199. * target and had its size accounted for, and thus can't have any
  200. * further relocations added to it.
  201. */
  202. bool used_as_reloc_target;
  203. /**
  204. * Boolean of whether we have encountered an error whilst building the relocation tree.
  205. */
  206. bool has_error;
  207. /**
  208. * Boolean of whether this buffer can be re-used
  209. */
  210. bool reusable;
  211. /**
  212. * Boolean of whether the GPU is definitely not accessing the buffer.
  213. *
  214. * This is only valid when reusable, since non-reusable
  215. * buffers are those that have been shared with other
  216. * processes, so we don't know their state.
  217. */
  218. bool idle;
  219. /**
  220. * Boolean of whether this buffer was allocated with userptr
  221. */
  222. bool is_userptr;
  223. /**
  224. * Size in bytes of this buffer and its relocation descendents.
  225. *
  226. * Used to avoid costly tree walking in
  227. * drm_intel_bufmgr_check_aperture in the common case.
  228. */
  229. int reloc_tree_size;
  230. /**
  231. * Number of potential fence registers required by this buffer and its
  232. * relocations.
  233. */
  234. int reloc_tree_fences;
  235. /** Flags that we may need to do the SW_FINISH ioctl on unmap. */
  236. bool mapped_cpu_write;
  237. };
  238. static unsigned int
  239. drm_intel_gem_estimate_batch_space(drm_intel_bo ** bo_array, int count);
  240. static unsigned int
  241. drm_intel_gem_compute_batch_space(drm_intel_bo ** bo_array, int count);
  242. static int
  243. drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
  244. uint32_t * swizzle_mode);
  245. static int
  246. drm_intel_gem_bo_set_tiling_internal(drm_intel_bo *bo,
  247. uint32_t tiling_mode,
  248. uint32_t stride);
  249. static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo,
  250. time_t time);
  251. static void drm_intel_gem_bo_unreference(drm_intel_bo *bo);
  252. static void drm_intel_gem_bo_free(drm_intel_bo *bo);
  253. static inline drm_intel_bo_gem *to_bo_gem(drm_intel_bo *bo)
  254. {
  255. return (drm_intel_bo_gem *)bo;
  256. }
  257. static unsigned long
  258. drm_intel_gem_bo_tile_size(drm_intel_bufmgr_gem *bufmgr_gem, unsigned long size,
  259. uint32_t *tiling_mode)
  260. {
  261. unsigned long min_size, max_size;
  262. unsigned long i;
  263. if (*tiling_mode == I915_TILING_NONE)
  264. return size;
  265. /* 965+ just need multiples of page size for tiling */
  266. if (bufmgr_gem->gen >= 4)
  267. return ROUND_UP_TO(size, 4096);
  268. /* Older chips need powers of two, of at least 512k or 1M */
  269. if (bufmgr_gem->gen == 3) {
  270. min_size = 1024*1024;
  271. max_size = 128*1024*1024;
  272. } else {
  273. min_size = 512*1024;
  274. max_size = 64*1024*1024;
  275. }
  276. if (size > max_size) {
  277. *tiling_mode = I915_TILING_NONE;
  278. return size;
  279. }
  280. /* Do we need to allocate every page for the fence? */
  281. if (bufmgr_gem->has_relaxed_fencing)
  282. return ROUND_UP_TO(size, 4096);
  283. for (i = min_size; i < size; i <<= 1)
  284. ;
  285. return i;
  286. }
  287. /*
  288. * Round a given pitch up to the minimum required for X tiling on a
  289. * given chip. We use 512 as the minimum to allow for a later tiling
  290. * change.
  291. */
  292. static unsigned long
  293. drm_intel_gem_bo_tile_pitch(drm_intel_bufmgr_gem *bufmgr_gem,
  294. unsigned long pitch, uint32_t *tiling_mode)
  295. {
  296. unsigned long tile_width;
  297. unsigned long i;
  298. /* If untiled, then just align it so that we can do rendering
  299. * to it with the 3D engine.
  300. */
  301. if (*tiling_mode == I915_TILING_NONE)
  302. return ALIGN(pitch, 64);
  303. if (*tiling_mode == I915_TILING_X
  304. || (IS_915(bufmgr_gem->pci_device)
  305. && *tiling_mode == I915_TILING_Y))
  306. tile_width = 512;
  307. else
  308. tile_width = 128;
  309. /* 965 is flexible */
  310. if (bufmgr_gem->gen >= 4)
  311. return ROUND_UP_TO(pitch, tile_width);
  312. /* The older hardware has a maximum pitch of 8192 with tiled
  313. * surfaces, so fallback to untiled if it's too large.
  314. */
  315. if (pitch > 8192) {
  316. *tiling_mode = I915_TILING_NONE;
  317. return ALIGN(pitch, 64);
  318. }
  319. /* Pre-965 needs power of two tile width */
  320. for (i = tile_width; i < pitch; i <<= 1)
  321. ;
  322. return i;
  323. }
  324. static struct drm_intel_gem_bo_bucket *
  325. drm_intel_gem_bo_bucket_for_size(drm_intel_bufmgr_gem *bufmgr_gem,
  326. unsigned long size)
  327. {
  328. int i;
  329. for (i = 0; i < bufmgr_gem->num_buckets; i++) {
  330. struct drm_intel_gem_bo_bucket *bucket =
  331. &bufmgr_gem->cache_bucket[i];
  332. if (bucket->size >= size) {
  333. return bucket;
  334. }
  335. }
  336. return NULL;
  337. }
  338. static void
  339. drm_intel_gem_dump_validation_list(drm_intel_bufmgr_gem *bufmgr_gem)
  340. {
  341. int i, j;
  342. for (i = 0; i < bufmgr_gem->exec_count; i++) {
  343. drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
  344. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  345. if (bo_gem->relocs == NULL && bo_gem->softpin_target == NULL) {
  346. DBG("%2d: %d %s(%s)\n", i, bo_gem->gem_handle,
  347. bo_gem->kflags & EXEC_OBJECT_PINNED ? "*" : "",
  348. bo_gem->name);
  349. continue;
  350. }
  351. for (j = 0; j < bo_gem->reloc_count; j++) {
  352. drm_intel_bo *target_bo = bo_gem->reloc_target_info[j].bo;
  353. drm_intel_bo_gem *target_gem =
  354. (drm_intel_bo_gem *) target_bo;
  355. DBG("%2d: %d %s(%s)@0x%08x %08x -> "
  356. "%d (%s)@0x%08x %08x + 0x%08x\n",
  357. i,
  358. bo_gem->gem_handle,
  359. bo_gem->kflags & EXEC_OBJECT_PINNED ? "*" : "",
  360. bo_gem->name,
  361. upper_32_bits(bo_gem->relocs[j].offset),
  362. lower_32_bits(bo_gem->relocs[j].offset),
  363. target_gem->gem_handle,
  364. target_gem->name,
  365. upper_32_bits(target_bo->offset64),
  366. lower_32_bits(target_bo->offset64),
  367. bo_gem->relocs[j].delta);
  368. }
  369. for (j = 0; j < bo_gem->softpin_target_count; j++) {
  370. drm_intel_bo *target_bo = bo_gem->softpin_target[j];
  371. drm_intel_bo_gem *target_gem =
  372. (drm_intel_bo_gem *) target_bo;
  373. DBG("%2d: %d %s(%s) -> "
  374. "%d *(%s)@0x%08x %08x\n",
  375. i,
  376. bo_gem->gem_handle,
  377. bo_gem->kflags & EXEC_OBJECT_PINNED ? "*" : "",
  378. bo_gem->name,
  379. target_gem->gem_handle,
  380. target_gem->name,
  381. upper_32_bits(target_bo->offset64),
  382. lower_32_bits(target_bo->offset64));
  383. }
  384. }
  385. }
  386. static inline void
  387. drm_intel_gem_bo_reference(drm_intel_bo *bo)
  388. {
  389. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  390. atomic_inc(&bo_gem->refcount);
  391. }
  392. /**
  393. * Adds the given buffer to the list of buffers to be validated (moved into the
  394. * appropriate memory type) with the next batch submission.
  395. *
  396. * If a buffer is validated multiple times in a batch submission, it ends up
  397. * with the intersection of the memory type flags and the union of the
  398. * access flags.
  399. */
  400. static void
  401. drm_intel_add_validate_buffer2(drm_intel_bo *bo, int need_fence)
  402. {
  403. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
  404. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
  405. int index;
  406. unsigned long flags;
  407. flags = 0;
  408. if (need_fence)
  409. flags |= EXEC_OBJECT_NEEDS_FENCE;
  410. if (bo_gem->validate_index != -1) {
  411. bufmgr_gem->exec2_objects[bo_gem->validate_index].flags |= flags;
  412. return;
  413. }
  414. /* Extend the array of validation entries as necessary. */
  415. if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
  416. int new_size = bufmgr_gem->exec_size * 2;
  417. if (new_size == 0)
  418. new_size = 5;
  419. bufmgr_gem->exec2_objects =
  420. realloc(bufmgr_gem->exec2_objects,
  421. sizeof(*bufmgr_gem->exec2_objects) * new_size);
  422. bufmgr_gem->exec_bos =
  423. realloc(bufmgr_gem->exec_bos,
  424. sizeof(*bufmgr_gem->exec_bos) * new_size);
  425. bufmgr_gem->exec_size = new_size;
  426. }
  427. index = bufmgr_gem->exec_count;
  428. bo_gem->validate_index = index;
  429. /* Fill in array entry */
  430. bufmgr_gem->exec2_objects[index].handle = bo_gem->gem_handle;
  431. bufmgr_gem->exec2_objects[index].relocation_count = bo_gem->reloc_count;
  432. bufmgr_gem->exec2_objects[index].relocs_ptr = (uintptr_t)bo_gem->relocs;
  433. bufmgr_gem->exec2_objects[index].alignment = bo->align;
  434. bufmgr_gem->exec2_objects[index].offset = bo->offset64;
  435. bufmgr_gem->exec2_objects[index].flags = bo_gem->kflags | flags;
  436. bufmgr_gem->exec2_objects[index].rsvd1 = 0;
  437. bufmgr_gem->exec2_objects[index].rsvd2 = 0;
  438. bufmgr_gem->exec_bos[index] = bo;
  439. bufmgr_gem->exec_count++;
  440. }
  441. #define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
  442. sizeof(uint32_t))
  443. static void
  444. drm_intel_bo_gem_set_in_aperture_size(drm_intel_bufmgr_gem *bufmgr_gem,
  445. drm_intel_bo_gem *bo_gem,
  446. unsigned int alignment)
  447. {
  448. unsigned int size;
  449. assert(!bo_gem->used_as_reloc_target);
  450. /* The older chipsets are far-less flexible in terms of tiling,
  451. * and require tiled buffer to be size aligned in the aperture.
  452. * This means that in the worst possible case we will need a hole
  453. * twice as large as the object in order for it to fit into the
  454. * aperture. Optimal packing is for wimps.
  455. */
  456. size = bo_gem->bo.size;
  457. if (bufmgr_gem->gen < 4 && bo_gem->tiling_mode != I915_TILING_NONE) {
  458. unsigned int min_size;
  459. if (bufmgr_gem->has_relaxed_fencing) {
  460. if (bufmgr_gem->gen == 3)
  461. min_size = 1024*1024;
  462. else
  463. min_size = 512*1024;
  464. while (min_size < size)
  465. min_size *= 2;
  466. } else
  467. min_size = size;
  468. /* Account for worst-case alignment. */
  469. alignment = MAX2(alignment, min_size);
  470. }
  471. bo_gem->reloc_tree_size = size + alignment;
  472. }
  473. static int
  474. drm_intel_setup_reloc_list(drm_intel_bo *bo)
  475. {
  476. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  477. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  478. unsigned int max_relocs = bufmgr_gem->max_relocs;
  479. if (bo->size / 4 < max_relocs)
  480. max_relocs = bo->size / 4;
  481. bo_gem->relocs = malloc(max_relocs *
  482. sizeof(struct drm_i915_gem_relocation_entry));
  483. bo_gem->reloc_target_info = malloc(max_relocs *
  484. sizeof(drm_intel_reloc_target));
  485. if (bo_gem->relocs == NULL || bo_gem->reloc_target_info == NULL) {
  486. bo_gem->has_error = true;
  487. free (bo_gem->relocs);
  488. bo_gem->relocs = NULL;
  489. free (bo_gem->reloc_target_info);
  490. bo_gem->reloc_target_info = NULL;
  491. return 1;
  492. }
  493. return 0;
  494. }
  495. static int
  496. drm_intel_gem_bo_busy(drm_intel_bo *bo)
  497. {
  498. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  499. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  500. struct drm_i915_gem_busy busy;
  501. int ret;
  502. if (bo_gem->reusable && bo_gem->idle)
  503. return false;
  504. memclear(busy);
  505. busy.handle = bo_gem->gem_handle;
  506. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
  507. if (ret == 0) {
  508. bo_gem->idle = !busy.busy;
  509. return busy.busy;
  510. } else {
  511. return false;
  512. }
  513. }
  514. static int
  515. drm_intel_gem_bo_madvise_internal(drm_intel_bufmgr_gem *bufmgr_gem,
  516. drm_intel_bo_gem *bo_gem, int state)
  517. {
  518. struct drm_i915_gem_madvise madv;
  519. memclear(madv);
  520. madv.handle = bo_gem->gem_handle;
  521. madv.madv = state;
  522. madv.retained = 1;
  523. drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv);
  524. return madv.retained;
  525. }
  526. static int
  527. drm_intel_gem_bo_madvise(drm_intel_bo *bo, int madv)
  528. {
  529. return drm_intel_gem_bo_madvise_internal
  530. ((drm_intel_bufmgr_gem *) bo->bufmgr,
  531. (drm_intel_bo_gem *) bo,
  532. madv);
  533. }
  534. /* drop the oldest entries that have been purged by the kernel */
  535. static void
  536. drm_intel_gem_bo_cache_purge_bucket(drm_intel_bufmgr_gem *bufmgr_gem,
  537. struct drm_intel_gem_bo_bucket *bucket)
  538. {
  539. while (!DRMLISTEMPTY(&bucket->head)) {
  540. drm_intel_bo_gem *bo_gem;
  541. bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
  542. bucket->head.next, head);
  543. if (drm_intel_gem_bo_madvise_internal
  544. (bufmgr_gem, bo_gem, I915_MADV_DONTNEED))
  545. break;
  546. DRMLISTDEL(&bo_gem->head);
  547. drm_intel_gem_bo_free(&bo_gem->bo);
  548. }
  549. }
  550. static drm_intel_bo *
  551. drm_intel_gem_bo_alloc_internal(drm_intel_bufmgr *bufmgr,
  552. const char *name,
  553. unsigned long size,
  554. unsigned long flags,
  555. uint32_t tiling_mode,
  556. unsigned long stride,
  557. unsigned int alignment)
  558. {
  559. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
  560. drm_intel_bo_gem *bo_gem;
  561. unsigned int page_size = getpagesize();
  562. int ret;
  563. struct drm_intel_gem_bo_bucket *bucket;
  564. bool alloc_from_cache;
  565. unsigned long bo_size;
  566. bool for_render = false;
  567. if (flags & BO_ALLOC_FOR_RENDER)
  568. for_render = true;
  569. /* Round the allocated size up to a power of two number of pages. */
  570. bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, size);
  571. /* If we don't have caching at this size, don't actually round the
  572. * allocation up.
  573. */
  574. if (bucket == NULL) {
  575. bo_size = size;
  576. if (bo_size < page_size)
  577. bo_size = page_size;
  578. } else {
  579. bo_size = bucket->size;
  580. }
  581. pthread_mutex_lock(&bufmgr_gem->lock);
  582. /* Get a buffer out of the cache if available */
  583. retry:
  584. alloc_from_cache = false;
  585. if (bucket != NULL && !DRMLISTEMPTY(&bucket->head)) {
  586. if (for_render) {
  587. /* Allocate new render-target BOs from the tail (MRU)
  588. * of the list, as it will likely be hot in the GPU
  589. * cache and in the aperture for us.
  590. */
  591. bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
  592. bucket->head.prev, head);
  593. DRMLISTDEL(&bo_gem->head);
  594. alloc_from_cache = true;
  595. bo_gem->bo.align = alignment;
  596. } else {
  597. assert(alignment == 0);
  598. /* For non-render-target BOs (where we're probably
  599. * going to map it first thing in order to fill it
  600. * with data), check if the last BO in the cache is
  601. * unbusy, and only reuse in that case. Otherwise,
  602. * allocating a new buffer is probably faster than
  603. * waiting for the GPU to finish.
  604. */
  605. bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
  606. bucket->head.next, head);
  607. if (!drm_intel_gem_bo_busy(&bo_gem->bo)) {
  608. alloc_from_cache = true;
  609. DRMLISTDEL(&bo_gem->head);
  610. }
  611. }
  612. if (alloc_from_cache) {
  613. if (!drm_intel_gem_bo_madvise_internal
  614. (bufmgr_gem, bo_gem, I915_MADV_WILLNEED)) {
  615. drm_intel_gem_bo_free(&bo_gem->bo);
  616. drm_intel_gem_bo_cache_purge_bucket(bufmgr_gem,
  617. bucket);
  618. goto retry;
  619. }
  620. if (drm_intel_gem_bo_set_tiling_internal(&bo_gem->bo,
  621. tiling_mode,
  622. stride)) {
  623. drm_intel_gem_bo_free(&bo_gem->bo);
  624. goto retry;
  625. }
  626. }
  627. }
  628. if (!alloc_from_cache) {
  629. struct drm_i915_gem_create create;
  630. bo_gem = calloc(1, sizeof(*bo_gem));
  631. if (!bo_gem)
  632. goto err;
  633. /* drm_intel_gem_bo_free calls DRMLISTDEL() for an uninitialized
  634. list (vma_list), so better set the list head here */
  635. DRMINITLISTHEAD(&bo_gem->vma_list);
  636. bo_gem->bo.size = bo_size;
  637. memclear(create);
  638. create.size = bo_size;
  639. ret = drmIoctl(bufmgr_gem->fd,
  640. DRM_IOCTL_I915_GEM_CREATE,
  641. &create);
  642. if (ret != 0) {
  643. free(bo_gem);
  644. goto err;
  645. }
  646. bo_gem->gem_handle = create.handle;
  647. HASH_ADD(handle_hh, bufmgr_gem->handle_table,
  648. gem_handle, sizeof(bo_gem->gem_handle),
  649. bo_gem);
  650. bo_gem->bo.handle = bo_gem->gem_handle;
  651. bo_gem->bo.bufmgr = bufmgr;
  652. bo_gem->bo.align = alignment;
  653. bo_gem->tiling_mode = I915_TILING_NONE;
  654. bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  655. bo_gem->stride = 0;
  656. if (drm_intel_gem_bo_set_tiling_internal(&bo_gem->bo,
  657. tiling_mode,
  658. stride))
  659. goto err_free;
  660. }
  661. bo_gem->name = name;
  662. atomic_set(&bo_gem->refcount, 1);
  663. bo_gem->validate_index = -1;
  664. bo_gem->reloc_tree_fences = 0;
  665. bo_gem->used_as_reloc_target = false;
  666. bo_gem->has_error = false;
  667. bo_gem->reusable = true;
  668. drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem, alignment);
  669. pthread_mutex_unlock(&bufmgr_gem->lock);
  670. DBG("bo_create: buf %d (%s) %ldb\n",
  671. bo_gem->gem_handle, bo_gem->name, size);
  672. return &bo_gem->bo;
  673. err_free:
  674. drm_intel_gem_bo_free(&bo_gem->bo);
  675. err:
  676. pthread_mutex_unlock(&bufmgr_gem->lock);
  677. return NULL;
  678. }
  679. static drm_intel_bo *
  680. drm_intel_gem_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
  681. const char *name,
  682. unsigned long size,
  683. unsigned int alignment)
  684. {
  685. return drm_intel_gem_bo_alloc_internal(bufmgr, name, size,
  686. BO_ALLOC_FOR_RENDER,
  687. I915_TILING_NONE, 0,
  688. alignment);
  689. }
  690. static drm_intel_bo *
  691. drm_intel_gem_bo_alloc(drm_intel_bufmgr *bufmgr,
  692. const char *name,
  693. unsigned long size,
  694. unsigned int alignment)
  695. {
  696. return drm_intel_gem_bo_alloc_internal(bufmgr, name, size, 0,
  697. I915_TILING_NONE, 0, 0);
  698. }
  699. static drm_intel_bo *
  700. drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
  701. int x, int y, int cpp, uint32_t *tiling_mode,
  702. unsigned long *pitch, unsigned long flags)
  703. {
  704. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
  705. unsigned long size, stride;
  706. uint32_t tiling;
  707. do {
  708. unsigned long aligned_y, height_alignment;
  709. tiling = *tiling_mode;
  710. /* If we're tiled, our allocations are in 8 or 32-row blocks,
  711. * so failure to align our height means that we won't allocate
  712. * enough pages.
  713. *
  714. * If we're untiled, we still have to align to 2 rows high
  715. * because the data port accesses 2x2 blocks even if the
  716. * bottom row isn't to be rendered, so failure to align means
  717. * we could walk off the end of the GTT and fault. This is
  718. * documented on 965, and may be the case on older chipsets
  719. * too so we try to be careful.
  720. */
  721. aligned_y = y;
  722. height_alignment = 2;
  723. if ((bufmgr_gem->gen == 2) && tiling != I915_TILING_NONE)
  724. height_alignment = 16;
  725. else if (tiling == I915_TILING_X
  726. || (IS_915(bufmgr_gem->pci_device)
  727. && tiling == I915_TILING_Y))
  728. height_alignment = 8;
  729. else if (tiling == I915_TILING_Y)
  730. height_alignment = 32;
  731. aligned_y = ALIGN(y, height_alignment);
  732. stride = x * cpp;
  733. stride = drm_intel_gem_bo_tile_pitch(bufmgr_gem, stride, tiling_mode);
  734. size = stride * aligned_y;
  735. size = drm_intel_gem_bo_tile_size(bufmgr_gem, size, tiling_mode);
  736. } while (*tiling_mode != tiling);
  737. *pitch = stride;
  738. if (tiling == I915_TILING_NONE)
  739. stride = 0;
  740. return drm_intel_gem_bo_alloc_internal(bufmgr, name, size, flags,
  741. tiling, stride, 0);
  742. }
  743. static drm_intel_bo *
  744. drm_intel_gem_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
  745. const char *name,
  746. void *addr,
  747. uint32_t tiling_mode,
  748. uint32_t stride,
  749. unsigned long size,
  750. unsigned long flags)
  751. {
  752. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
  753. drm_intel_bo_gem *bo_gem;
  754. int ret;
  755. struct drm_i915_gem_userptr userptr;
  756. /* Tiling with userptr surfaces is not supported
  757. * on all hardware so refuse it for time being.
  758. */
  759. if (tiling_mode != I915_TILING_NONE)
  760. return NULL;
  761. bo_gem = calloc(1, sizeof(*bo_gem));
  762. if (!bo_gem)
  763. return NULL;
  764. atomic_set(&bo_gem->refcount, 1);
  765. DRMINITLISTHEAD(&bo_gem->vma_list);
  766. bo_gem->bo.size = size;
  767. memclear(userptr);
  768. userptr.user_ptr = (__u64)((unsigned long)addr);
  769. userptr.user_size = size;
  770. userptr.flags = flags;
  771. ret = drmIoctl(bufmgr_gem->fd,
  772. DRM_IOCTL_I915_GEM_USERPTR,
  773. &userptr);
  774. if (ret != 0) {
  775. DBG("bo_create_userptr: "
  776. "ioctl failed with user ptr %p size 0x%lx, "
  777. "user flags 0x%lx\n", addr, size, flags);
  778. free(bo_gem);
  779. return NULL;
  780. }
  781. pthread_mutex_lock(&bufmgr_gem->lock);
  782. bo_gem->gem_handle = userptr.handle;
  783. bo_gem->bo.handle = bo_gem->gem_handle;
  784. bo_gem->bo.bufmgr = bufmgr;
  785. bo_gem->is_userptr = true;
  786. bo_gem->bo.virtual = addr;
  787. /* Save the address provided by user */
  788. bo_gem->user_virtual = addr;
  789. bo_gem->tiling_mode = I915_TILING_NONE;
  790. bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
  791. bo_gem->stride = 0;
  792. HASH_ADD(handle_hh, bufmgr_gem->handle_table,
  793. gem_handle, sizeof(bo_gem->gem_handle),
  794. bo_gem);
  795. bo_gem->name = name;
  796. bo_gem->validate_index = -1;
  797. bo_gem->reloc_tree_fences = 0;
  798. bo_gem->used_as_reloc_target = false;
  799. bo_gem->has_error = false;
  800. bo_gem->reusable = false;
  801. drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem, 0);
  802. pthread_mutex_unlock(&bufmgr_gem->lock);
  803. DBG("bo_create_userptr: "
  804. "ptr %p buf %d (%s) size %ldb, stride 0x%x, tile mode %d\n",
  805. addr, bo_gem->gem_handle, bo_gem->name,
  806. size, stride, tiling_mode);
  807. return &bo_gem->bo;
  808. }
  809. static bool
  810. has_userptr(drm_intel_bufmgr_gem *bufmgr_gem)
  811. {
  812. int ret;
  813. void *ptr;
  814. long pgsz;
  815. struct drm_i915_gem_userptr userptr;
  816. pgsz = sysconf(_SC_PAGESIZE);
  817. assert(pgsz > 0);
  818. ret = posix_memalign(&ptr, pgsz, pgsz);
  819. if (ret) {
  820. DBG("Failed to get a page (%ld) for userptr detection!\n",
  821. pgsz);
  822. return false;
  823. }
  824. memclear(userptr);
  825. userptr.user_ptr = (__u64)(unsigned long)ptr;
  826. userptr.user_size = pgsz;
  827. retry:
  828. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
  829. if (ret) {
  830. if (errno == ENODEV && userptr.flags == 0) {
  831. userptr.flags = I915_USERPTR_UNSYNCHRONIZED;
  832. goto retry;
  833. }
  834. free(ptr);
  835. return false;
  836. }
  837. /* We don't release the userptr bo here as we want to keep the
  838. * kernel mm tracking alive for our lifetime. The first time we
  839. * create a userptr object the kernel has to install a mmu_notifer
  840. * which is a heavyweight operation (e.g. it requires taking all
  841. * mm_locks and stop_machine()).
  842. */
  843. bufmgr_gem->userptr_active.ptr = ptr;
  844. bufmgr_gem->userptr_active.handle = userptr.handle;
  845. return true;
  846. }
  847. static drm_intel_bo *
  848. check_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
  849. const char *name,
  850. void *addr,
  851. uint32_t tiling_mode,
  852. uint32_t stride,
  853. unsigned long size,
  854. unsigned long flags)
  855. {
  856. if (has_userptr((drm_intel_bufmgr_gem *)bufmgr))
  857. bufmgr->bo_alloc_userptr = drm_intel_gem_bo_alloc_userptr;
  858. else
  859. bufmgr->bo_alloc_userptr = NULL;
  860. return drm_intel_bo_alloc_userptr(bufmgr, name, addr,
  861. tiling_mode, stride, size, flags);
  862. }
  863. static int get_tiling_mode(drm_intel_bufmgr_gem *bufmgr_gem,
  864. uint32_t gem_handle,
  865. uint32_t *tiling_mode,
  866. uint32_t *swizzle_mode)
  867. {
  868. struct drm_i915_gem_get_tiling get_tiling = {
  869. .handle = gem_handle,
  870. };
  871. int ret;
  872. ret = drmIoctl(bufmgr_gem->fd,
  873. DRM_IOCTL_I915_GEM_GET_TILING,
  874. &get_tiling);
  875. if (ret != 0 && errno != EOPNOTSUPP)
  876. return ret;
  877. *tiling_mode = get_tiling.tiling_mode;
  878. *swizzle_mode = get_tiling.swizzle_mode;
  879. return 0;
  880. }
  881. /**
  882. * Returns a drm_intel_bo wrapping the given buffer object handle.
  883. *
  884. * This can be used when one application needs to pass a buffer object
  885. * to another.
  886. */
  887. drm_public drm_intel_bo *
  888. drm_intel_bo_gem_create_from_name(drm_intel_bufmgr *bufmgr,
  889. const char *name,
  890. unsigned int handle)
  891. {
  892. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
  893. drm_intel_bo_gem *bo_gem;
  894. int ret;
  895. struct drm_gem_open open_arg;
  896. /* At the moment most applications only have a few named bo.
  897. * For instance, in a DRI client only the render buffers passed
  898. * between X and the client are named. And since X returns the
  899. * alternating names for the front/back buffer a linear search
  900. * provides a sufficiently fast match.
  901. */
  902. pthread_mutex_lock(&bufmgr_gem->lock);
  903. HASH_FIND(name_hh, bufmgr_gem->name_table,
  904. &handle, sizeof(handle), bo_gem);
  905. if (bo_gem) {
  906. drm_intel_gem_bo_reference(&bo_gem->bo);
  907. goto out;
  908. }
  909. memclear(open_arg);
  910. open_arg.name = handle;
  911. ret = drmIoctl(bufmgr_gem->fd,
  912. DRM_IOCTL_GEM_OPEN,
  913. &open_arg);
  914. if (ret != 0) {
  915. DBG("Couldn't reference %s handle 0x%08x: %s\n",
  916. name, handle, strerror(errno));
  917. bo_gem = NULL;
  918. goto out;
  919. }
  920. /* Now see if someone has used a prime handle to get this
  921. * object from the kernel before by looking through the list
  922. * again for a matching gem_handle
  923. */
  924. HASH_FIND(handle_hh, bufmgr_gem->handle_table,
  925. &open_arg.handle, sizeof(open_arg.handle), bo_gem);
  926. if (bo_gem) {
  927. drm_intel_gem_bo_reference(&bo_gem->bo);
  928. goto out;
  929. }
  930. bo_gem = calloc(1, sizeof(*bo_gem));
  931. if (!bo_gem)
  932. goto out;
  933. atomic_set(&bo_gem->refcount, 1);
  934. DRMINITLISTHEAD(&bo_gem->vma_list);
  935. bo_gem->bo.size = open_arg.size;
  936. bo_gem->bo.offset = 0;
  937. bo_gem->bo.offset64 = 0;
  938. bo_gem->bo.virtual = NULL;
  939. bo_gem->bo.bufmgr = bufmgr;
  940. bo_gem->name = name;
  941. bo_gem->validate_index = -1;
  942. bo_gem->gem_handle = open_arg.handle;
  943. bo_gem->bo.handle = open_arg.handle;
  944. bo_gem->global_name = handle;
  945. bo_gem->reusable = false;
  946. HASH_ADD(handle_hh, bufmgr_gem->handle_table,
  947. gem_handle, sizeof(bo_gem->gem_handle), bo_gem);
  948. HASH_ADD(name_hh, bufmgr_gem->name_table,
  949. global_name, sizeof(bo_gem->global_name), bo_gem);
  950. ret = get_tiling_mode(bufmgr_gem, bo_gem->gem_handle,
  951. &bo_gem->tiling_mode, &bo_gem->swizzle_mode);
  952. if (ret != 0)
  953. goto err_unref;
  954. /* XXX stride is unknown */
  955. drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem, 0);
  956. DBG("bo_create_from_handle: %d (%s)\n", handle, bo_gem->name);
  957. out:
  958. pthread_mutex_unlock(&bufmgr_gem->lock);
  959. return &bo_gem->bo;
  960. err_unref:
  961. drm_intel_gem_bo_free(&bo_gem->bo);
  962. pthread_mutex_unlock(&bufmgr_gem->lock);
  963. return NULL;
  964. }
  965. static void
  966. drm_intel_gem_bo_free(drm_intel_bo *bo)
  967. {
  968. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  969. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  970. int ret;
  971. DRMLISTDEL(&bo_gem->vma_list);
  972. if (bo_gem->mem_virtual) {
  973. VG(VALGRIND_FREELIKE_BLOCK(bo_gem->mem_virtual, 0));
  974. drm_munmap(bo_gem->mem_virtual, bo_gem->bo.size);
  975. bufmgr_gem->vma_count--;
  976. }
  977. if (bo_gem->wc_virtual) {
  978. VG(VALGRIND_FREELIKE_BLOCK(bo_gem->wc_virtual, 0));
  979. drm_munmap(bo_gem->wc_virtual, bo_gem->bo.size);
  980. bufmgr_gem->vma_count--;
  981. }
  982. if (bo_gem->gtt_virtual) {
  983. drm_munmap(bo_gem->gtt_virtual, bo_gem->bo.size);
  984. bufmgr_gem->vma_count--;
  985. }
  986. if (bo_gem->global_name)
  987. HASH_DELETE(name_hh, bufmgr_gem->name_table, bo_gem);
  988. HASH_DELETE(handle_hh, bufmgr_gem->handle_table, bo_gem);
  989. /* Close this object */
  990. ret = drmCloseBufferHandle(bufmgr_gem->fd, bo_gem->gem_handle);
  991. if (ret != 0) {
  992. DBG("drmCloseBufferHandle %d failed (%s): %s\n",
  993. bo_gem->gem_handle, bo_gem->name, strerror(errno));
  994. }
  995. free(bo);
  996. }
  997. static void
  998. drm_intel_gem_bo_mark_mmaps_incoherent(drm_intel_bo *bo)
  999. {
  1000. #if HAVE_VALGRIND
  1001. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1002. if (bo_gem->mem_virtual)
  1003. VALGRIND_MAKE_MEM_NOACCESS(bo_gem->mem_virtual, bo->size);
  1004. if (bo_gem->wc_virtual)
  1005. VALGRIND_MAKE_MEM_NOACCESS(bo_gem->wc_virtual, bo->size);
  1006. if (bo_gem->gtt_virtual)
  1007. VALGRIND_MAKE_MEM_NOACCESS(bo_gem->gtt_virtual, bo->size);
  1008. #endif
  1009. }
  1010. /** Frees all cached buffers significantly older than @time. */
  1011. static void
  1012. drm_intel_gem_cleanup_bo_cache(drm_intel_bufmgr_gem *bufmgr_gem, time_t time)
  1013. {
  1014. int i;
  1015. if (bufmgr_gem->time == time)
  1016. return;
  1017. for (i = 0; i < bufmgr_gem->num_buckets; i++) {
  1018. struct drm_intel_gem_bo_bucket *bucket =
  1019. &bufmgr_gem->cache_bucket[i];
  1020. while (!DRMLISTEMPTY(&bucket->head)) {
  1021. drm_intel_bo_gem *bo_gem;
  1022. bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
  1023. bucket->head.next, head);
  1024. if (time - bo_gem->free_time <= 1)
  1025. break;
  1026. DRMLISTDEL(&bo_gem->head);
  1027. drm_intel_gem_bo_free(&bo_gem->bo);
  1028. }
  1029. }
  1030. bufmgr_gem->time = time;
  1031. }
  1032. static void drm_intel_gem_bo_purge_vma_cache(drm_intel_bufmgr_gem *bufmgr_gem)
  1033. {
  1034. int limit;
  1035. DBG("%s: cached=%d, open=%d, limit=%d\n", __FUNCTION__,
  1036. bufmgr_gem->vma_count, bufmgr_gem->vma_open, bufmgr_gem->vma_max);
  1037. if (bufmgr_gem->vma_max < 0)
  1038. return;
  1039. /* We may need to evict a few entries in order to create new mmaps */
  1040. limit = bufmgr_gem->vma_max - 2*bufmgr_gem->vma_open;
  1041. if (limit < 0)
  1042. limit = 0;
  1043. while (bufmgr_gem->vma_count > limit) {
  1044. drm_intel_bo_gem *bo_gem;
  1045. bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
  1046. bufmgr_gem->vma_cache.next,
  1047. vma_list);
  1048. assert(bo_gem->map_count == 0);
  1049. DRMLISTDELINIT(&bo_gem->vma_list);
  1050. if (bo_gem->mem_virtual) {
  1051. drm_munmap(bo_gem->mem_virtual, bo_gem->bo.size);
  1052. bo_gem->mem_virtual = NULL;
  1053. bufmgr_gem->vma_count--;
  1054. }
  1055. if (bo_gem->wc_virtual) {
  1056. drm_munmap(bo_gem->wc_virtual, bo_gem->bo.size);
  1057. bo_gem->wc_virtual = NULL;
  1058. bufmgr_gem->vma_count--;
  1059. }
  1060. if (bo_gem->gtt_virtual) {
  1061. drm_munmap(bo_gem->gtt_virtual, bo_gem->bo.size);
  1062. bo_gem->gtt_virtual = NULL;
  1063. bufmgr_gem->vma_count--;
  1064. }
  1065. }
  1066. }
  1067. static void drm_intel_gem_bo_close_vma(drm_intel_bufmgr_gem *bufmgr_gem,
  1068. drm_intel_bo_gem *bo_gem)
  1069. {
  1070. bufmgr_gem->vma_open--;
  1071. DRMLISTADDTAIL(&bo_gem->vma_list, &bufmgr_gem->vma_cache);
  1072. if (bo_gem->mem_virtual)
  1073. bufmgr_gem->vma_count++;
  1074. if (bo_gem->wc_virtual)
  1075. bufmgr_gem->vma_count++;
  1076. if (bo_gem->gtt_virtual)
  1077. bufmgr_gem->vma_count++;
  1078. drm_intel_gem_bo_purge_vma_cache(bufmgr_gem);
  1079. }
  1080. static void drm_intel_gem_bo_open_vma(drm_intel_bufmgr_gem *bufmgr_gem,
  1081. drm_intel_bo_gem *bo_gem)
  1082. {
  1083. bufmgr_gem->vma_open++;
  1084. DRMLISTDEL(&bo_gem->vma_list);
  1085. if (bo_gem->mem_virtual)
  1086. bufmgr_gem->vma_count--;
  1087. if (bo_gem->wc_virtual)
  1088. bufmgr_gem->vma_count--;
  1089. if (bo_gem->gtt_virtual)
  1090. bufmgr_gem->vma_count--;
  1091. drm_intel_gem_bo_purge_vma_cache(bufmgr_gem);
  1092. }
  1093. static void
  1094. drm_intel_gem_bo_unreference_final(drm_intel_bo *bo, time_t time)
  1095. {
  1096. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1097. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1098. struct drm_intel_gem_bo_bucket *bucket;
  1099. int i;
  1100. /* Unreference all the target buffers */
  1101. for (i = 0; i < bo_gem->reloc_count; i++) {
  1102. if (bo_gem->reloc_target_info[i].bo != bo) {
  1103. drm_intel_gem_bo_unreference_locked_timed(bo_gem->
  1104. reloc_target_info[i].bo,
  1105. time);
  1106. }
  1107. }
  1108. for (i = 0; i < bo_gem->softpin_target_count; i++)
  1109. drm_intel_gem_bo_unreference_locked_timed(bo_gem->softpin_target[i],
  1110. time);
  1111. bo_gem->kflags = 0;
  1112. bo_gem->reloc_count = 0;
  1113. bo_gem->used_as_reloc_target = false;
  1114. bo_gem->softpin_target_count = 0;
  1115. DBG("bo_unreference final: %d (%s)\n",
  1116. bo_gem->gem_handle, bo_gem->name);
  1117. /* release memory associated with this object */
  1118. if (bo_gem->reloc_target_info) {
  1119. free(bo_gem->reloc_target_info);
  1120. bo_gem->reloc_target_info = NULL;
  1121. }
  1122. if (bo_gem->relocs) {
  1123. free(bo_gem->relocs);
  1124. bo_gem->relocs = NULL;
  1125. }
  1126. if (bo_gem->softpin_target) {
  1127. free(bo_gem->softpin_target);
  1128. bo_gem->softpin_target = NULL;
  1129. bo_gem->softpin_target_size = 0;
  1130. }
  1131. /* Clear any left-over mappings */
  1132. if (bo_gem->map_count) {
  1133. DBG("bo freed with non-zero map-count %d\n", bo_gem->map_count);
  1134. bo_gem->map_count = 0;
  1135. drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
  1136. drm_intel_gem_bo_mark_mmaps_incoherent(bo);
  1137. }
  1138. bucket = drm_intel_gem_bo_bucket_for_size(bufmgr_gem, bo->size);
  1139. /* Put the buffer into our internal cache for reuse if we can. */
  1140. if (bufmgr_gem->bo_reuse && bo_gem->reusable && bucket != NULL &&
  1141. drm_intel_gem_bo_madvise_internal(bufmgr_gem, bo_gem,
  1142. I915_MADV_DONTNEED)) {
  1143. bo_gem->free_time = time;
  1144. bo_gem->name = NULL;
  1145. bo_gem->validate_index = -1;
  1146. DRMLISTADDTAIL(&bo_gem->head, &bucket->head);
  1147. } else {
  1148. drm_intel_gem_bo_free(bo);
  1149. }
  1150. }
  1151. static void drm_intel_gem_bo_unreference_locked_timed(drm_intel_bo *bo,
  1152. time_t time)
  1153. {
  1154. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1155. assert(atomic_read(&bo_gem->refcount) > 0);
  1156. if (atomic_dec_and_test(&bo_gem->refcount))
  1157. drm_intel_gem_bo_unreference_final(bo, time);
  1158. }
  1159. static void drm_intel_gem_bo_unreference(drm_intel_bo *bo)
  1160. {
  1161. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1162. drm_intel_bufmgr_gem *bufmgr_gem;
  1163. struct timespec time;
  1164. assert(atomic_read(&bo_gem->refcount) > 0);
  1165. if (atomic_add_unless(&bo_gem->refcount, -1, 1))
  1166. return;
  1167. bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1168. clock_gettime(CLOCK_MONOTONIC, &time);
  1169. pthread_mutex_lock(&bufmgr_gem->lock);
  1170. if (atomic_dec_and_test(&bo_gem->refcount)) {
  1171. drm_intel_gem_bo_unreference_final(bo, time.tv_sec);
  1172. drm_intel_gem_cleanup_bo_cache(bufmgr_gem, time.tv_sec);
  1173. }
  1174. pthread_mutex_unlock(&bufmgr_gem->lock);
  1175. }
  1176. static int drm_intel_gem_bo_map(drm_intel_bo *bo, int write_enable)
  1177. {
  1178. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1179. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1180. struct drm_i915_gem_set_domain set_domain;
  1181. int ret;
  1182. if (bo_gem->is_userptr) {
  1183. /* Return the same user ptr */
  1184. bo->virtual = bo_gem->user_virtual;
  1185. return 0;
  1186. }
  1187. pthread_mutex_lock(&bufmgr_gem->lock);
  1188. if (bo_gem->map_count++ == 0)
  1189. drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
  1190. if (!bo_gem->mem_virtual) {
  1191. struct drm_i915_gem_mmap mmap_arg;
  1192. DBG("bo_map: %d (%s), map_count=%d\n",
  1193. bo_gem->gem_handle, bo_gem->name, bo_gem->map_count);
  1194. memclear(mmap_arg);
  1195. mmap_arg.handle = bo_gem->gem_handle;
  1196. mmap_arg.size = bo->size;
  1197. ret = drmIoctl(bufmgr_gem->fd,
  1198. DRM_IOCTL_I915_GEM_MMAP,
  1199. &mmap_arg);
  1200. if (ret != 0) {
  1201. ret = -errno;
  1202. DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
  1203. __FILE__, __LINE__, bo_gem->gem_handle,
  1204. bo_gem->name, strerror(errno));
  1205. if (--bo_gem->map_count == 0)
  1206. drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
  1207. pthread_mutex_unlock(&bufmgr_gem->lock);
  1208. return ret;
  1209. }
  1210. VG(VALGRIND_MALLOCLIKE_BLOCK(mmap_arg.addr_ptr, mmap_arg.size, 0, 1));
  1211. bo_gem->mem_virtual = (void *)(uintptr_t) mmap_arg.addr_ptr;
  1212. }
  1213. DBG("bo_map: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name,
  1214. bo_gem->mem_virtual);
  1215. bo->virtual = bo_gem->mem_virtual;
  1216. memclear(set_domain);
  1217. set_domain.handle = bo_gem->gem_handle;
  1218. set_domain.read_domains = I915_GEM_DOMAIN_CPU;
  1219. if (write_enable)
  1220. set_domain.write_domain = I915_GEM_DOMAIN_CPU;
  1221. else
  1222. set_domain.write_domain = 0;
  1223. ret = drmIoctl(bufmgr_gem->fd,
  1224. DRM_IOCTL_I915_GEM_SET_DOMAIN,
  1225. &set_domain);
  1226. if (ret != 0) {
  1227. DBG("%s:%d: Error setting to CPU domain %d: %s\n",
  1228. __FILE__, __LINE__, bo_gem->gem_handle,
  1229. strerror(errno));
  1230. }
  1231. if (write_enable)
  1232. bo_gem->mapped_cpu_write = true;
  1233. drm_intel_gem_bo_mark_mmaps_incoherent(bo);
  1234. VG(VALGRIND_MAKE_MEM_DEFINED(bo_gem->mem_virtual, bo->size));
  1235. pthread_mutex_unlock(&bufmgr_gem->lock);
  1236. return 0;
  1237. }
  1238. static int
  1239. map_gtt(drm_intel_bo *bo)
  1240. {
  1241. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1242. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1243. int ret;
  1244. if (bo_gem->is_userptr)
  1245. return -EINVAL;
  1246. if (bo_gem->map_count++ == 0)
  1247. drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
  1248. /* Get a mapping of the buffer if we haven't before. */
  1249. if (bo_gem->gtt_virtual == NULL) {
  1250. struct drm_i915_gem_mmap_gtt mmap_arg;
  1251. DBG("bo_map_gtt: mmap %d (%s), map_count=%d\n",
  1252. bo_gem->gem_handle, bo_gem->name, bo_gem->map_count);
  1253. memclear(mmap_arg);
  1254. mmap_arg.handle = bo_gem->gem_handle;
  1255. /* Get the fake offset back... */
  1256. ret = drmIoctl(bufmgr_gem->fd,
  1257. DRM_IOCTL_I915_GEM_MMAP_GTT,
  1258. &mmap_arg);
  1259. if (ret != 0) {
  1260. ret = -errno;
  1261. DBG("%s:%d: Error preparing buffer map %d (%s): %s .\n",
  1262. __FILE__, __LINE__,
  1263. bo_gem->gem_handle, bo_gem->name,
  1264. strerror(errno));
  1265. if (--bo_gem->map_count == 0)
  1266. drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
  1267. return ret;
  1268. }
  1269. /* and mmap it */
  1270. bo_gem->gtt_virtual = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE,
  1271. MAP_SHARED, bufmgr_gem->fd,
  1272. mmap_arg.offset);
  1273. if (bo_gem->gtt_virtual == MAP_FAILED) {
  1274. bo_gem->gtt_virtual = NULL;
  1275. ret = -errno;
  1276. DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
  1277. __FILE__, __LINE__,
  1278. bo_gem->gem_handle, bo_gem->name,
  1279. strerror(errno));
  1280. if (--bo_gem->map_count == 0)
  1281. drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
  1282. return ret;
  1283. }
  1284. }
  1285. bo->virtual = bo_gem->gtt_virtual;
  1286. DBG("bo_map_gtt: %d (%s) -> %p\n", bo_gem->gem_handle, bo_gem->name,
  1287. bo_gem->gtt_virtual);
  1288. return 0;
  1289. }
  1290. drm_public int
  1291. drm_intel_gem_bo_map_gtt(drm_intel_bo *bo)
  1292. {
  1293. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1294. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1295. struct drm_i915_gem_set_domain set_domain;
  1296. int ret;
  1297. pthread_mutex_lock(&bufmgr_gem->lock);
  1298. ret = map_gtt(bo);
  1299. if (ret) {
  1300. pthread_mutex_unlock(&bufmgr_gem->lock);
  1301. return ret;
  1302. }
  1303. /* Now move it to the GTT domain so that the GPU and CPU
  1304. * caches are flushed and the GPU isn't actively using the
  1305. * buffer.
  1306. *
  1307. * The pagefault handler does this domain change for us when
  1308. * it has unbound the BO from the GTT, but it's up to us to
  1309. * tell it when we're about to use things if we had done
  1310. * rendering and it still happens to be bound to the GTT.
  1311. */
  1312. memclear(set_domain);
  1313. set_domain.handle = bo_gem->gem_handle;
  1314. set_domain.read_domains = I915_GEM_DOMAIN_GTT;
  1315. set_domain.write_domain = I915_GEM_DOMAIN_GTT;
  1316. ret = drmIoctl(bufmgr_gem->fd,
  1317. DRM_IOCTL_I915_GEM_SET_DOMAIN,
  1318. &set_domain);
  1319. if (ret != 0) {
  1320. DBG("%s:%d: Error setting domain %d: %s\n",
  1321. __FILE__, __LINE__, bo_gem->gem_handle,
  1322. strerror(errno));
  1323. }
  1324. drm_intel_gem_bo_mark_mmaps_incoherent(bo);
  1325. VG(VALGRIND_MAKE_MEM_DEFINED(bo_gem->gtt_virtual, bo->size));
  1326. pthread_mutex_unlock(&bufmgr_gem->lock);
  1327. return 0;
  1328. }
  1329. /**
  1330. * Performs a mapping of the buffer object like the normal GTT
  1331. * mapping, but avoids waiting for the GPU to be done reading from or
  1332. * rendering to the buffer.
  1333. *
  1334. * This is used in the implementation of GL_ARB_map_buffer_range: The
  1335. * user asks to create a buffer, then does a mapping, fills some
  1336. * space, runs a drawing command, then asks to map it again without
  1337. * synchronizing because it guarantees that it won't write over the
  1338. * data that the GPU is busy using (or, more specifically, that if it
  1339. * does write over the data, it acknowledges that rendering is
  1340. * undefined).
  1341. */
  1342. drm_public int
  1343. drm_intel_gem_bo_map_unsynchronized(drm_intel_bo *bo)
  1344. {
  1345. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1346. #if HAVE_VALGRIND
  1347. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1348. #endif
  1349. int ret;
  1350. /* If the CPU cache isn't coherent with the GTT, then use a
  1351. * regular synchronized mapping. The problem is that we don't
  1352. * track where the buffer was last used on the CPU side in
  1353. * terms of drm_intel_bo_map vs drm_intel_gem_bo_map_gtt, so
  1354. * we would potentially corrupt the buffer even when the user
  1355. * does reasonable things.
  1356. */
  1357. if (!bufmgr_gem->has_llc)
  1358. return drm_intel_gem_bo_map_gtt(bo);
  1359. pthread_mutex_lock(&bufmgr_gem->lock);
  1360. ret = map_gtt(bo);
  1361. if (ret == 0) {
  1362. drm_intel_gem_bo_mark_mmaps_incoherent(bo);
  1363. VG(VALGRIND_MAKE_MEM_DEFINED(bo_gem->gtt_virtual, bo->size));
  1364. }
  1365. pthread_mutex_unlock(&bufmgr_gem->lock);
  1366. return ret;
  1367. }
  1368. static int drm_intel_gem_bo_unmap(drm_intel_bo *bo)
  1369. {
  1370. drm_intel_bufmgr_gem *bufmgr_gem;
  1371. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1372. int ret = 0;
  1373. if (bo == NULL)
  1374. return 0;
  1375. if (bo_gem->is_userptr)
  1376. return 0;
  1377. bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1378. pthread_mutex_lock(&bufmgr_gem->lock);
  1379. if (bo_gem->map_count <= 0) {
  1380. DBG("attempted to unmap an unmapped bo\n");
  1381. pthread_mutex_unlock(&bufmgr_gem->lock);
  1382. /* Preserve the old behaviour of just treating this as a
  1383. * no-op rather than reporting the error.
  1384. */
  1385. return 0;
  1386. }
  1387. if (bo_gem->mapped_cpu_write) {
  1388. struct drm_i915_gem_sw_finish sw_finish;
  1389. /* Cause a flush to happen if the buffer's pinned for
  1390. * scanout, so the results show up in a timely manner.
  1391. * Unlike GTT set domains, this only does work if the
  1392. * buffer should be scanout-related.
  1393. */
  1394. memclear(sw_finish);
  1395. sw_finish.handle = bo_gem->gem_handle;
  1396. ret = drmIoctl(bufmgr_gem->fd,
  1397. DRM_IOCTL_I915_GEM_SW_FINISH,
  1398. &sw_finish);
  1399. ret = ret == -1 ? -errno : 0;
  1400. bo_gem->mapped_cpu_write = false;
  1401. }
  1402. /* We need to unmap after every innovation as we cannot track
  1403. * an open vma for every bo as that will exhaust the system
  1404. * limits and cause later failures.
  1405. */
  1406. if (--bo_gem->map_count == 0) {
  1407. drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
  1408. drm_intel_gem_bo_mark_mmaps_incoherent(bo);
  1409. bo->virtual = NULL;
  1410. }
  1411. pthread_mutex_unlock(&bufmgr_gem->lock);
  1412. return ret;
  1413. }
  1414. drm_public int
  1415. drm_intel_gem_bo_unmap_gtt(drm_intel_bo *bo)
  1416. {
  1417. return drm_intel_gem_bo_unmap(bo);
  1418. }
  1419. static bool is_cache_coherent(drm_intel_bo *bo)
  1420. {
  1421. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1422. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1423. struct drm_i915_gem_caching arg = {};
  1424. arg.handle = bo_gem->gem_handle;
  1425. if (drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_GET_CACHING, &arg))
  1426. assert(false);
  1427. return arg.caching != I915_CACHING_NONE;
  1428. }
  1429. static void set_domain(drm_intel_bo *bo, uint32_t read, uint32_t write)
  1430. {
  1431. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1432. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1433. struct drm_i915_gem_set_domain arg = {};
  1434. arg.handle = bo_gem->gem_handle;
  1435. arg.read_domains = read;
  1436. arg.write_domain = write;
  1437. if (drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &arg))
  1438. assert(false);
  1439. }
  1440. static int mmap_write(drm_intel_bo *bo, unsigned long offset,
  1441. unsigned long length, const void *buf)
  1442. {
  1443. void *map = NULL;
  1444. if (!length)
  1445. return 0;
  1446. if (is_cache_coherent(bo)) {
  1447. map = drm_intel_gem_bo_map__cpu(bo);
  1448. if (map)
  1449. set_domain(bo, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
  1450. }
  1451. if (!map) {
  1452. map = drm_intel_gem_bo_map__wc(bo);
  1453. if (map)
  1454. set_domain(bo, I915_GEM_DOMAIN_WC, I915_GEM_DOMAIN_WC);
  1455. }
  1456. assert(map);
  1457. memcpy((char *)map + offset, buf, length);
  1458. drm_intel_gem_bo_unmap(bo);
  1459. return 0;
  1460. }
  1461. static int mmap_read(drm_intel_bo *bo, unsigned long offset,
  1462. unsigned long length, void *buf)
  1463. {
  1464. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1465. void *map = NULL;
  1466. if (!length)
  1467. return 0;
  1468. if (bufmgr_gem->has_llc || is_cache_coherent(bo)) {
  1469. map = drm_intel_gem_bo_map__cpu(bo);
  1470. if (map)
  1471. set_domain(bo, I915_GEM_DOMAIN_CPU, 0);
  1472. }
  1473. if (!map) {
  1474. map = drm_intel_gem_bo_map__wc(bo);
  1475. if (map)
  1476. set_domain(bo, I915_GEM_DOMAIN_WC, 0);
  1477. }
  1478. assert(map);
  1479. memcpy(buf, (char *)map + offset, length);
  1480. drm_intel_gem_bo_unmap(bo);
  1481. return 0;
  1482. }
  1483. static int
  1484. drm_intel_gem_bo_subdata(drm_intel_bo *bo, unsigned long offset,
  1485. unsigned long size, const void *data)
  1486. {
  1487. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1488. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1489. struct drm_i915_gem_pwrite pwrite;
  1490. int ret;
  1491. if (bo_gem->is_userptr)
  1492. return -EINVAL;
  1493. memclear(pwrite);
  1494. pwrite.handle = bo_gem->gem_handle;
  1495. pwrite.offset = offset;
  1496. pwrite.size = size;
  1497. pwrite.data_ptr = (uint64_t) (uintptr_t) data;
  1498. ret = drmIoctl(bufmgr_gem->fd,
  1499. DRM_IOCTL_I915_GEM_PWRITE,
  1500. &pwrite);
  1501. if (ret)
  1502. ret = -errno;
  1503. if (ret != 0 && ret != -EOPNOTSUPP) {
  1504. DBG("%s:%d: Error writing data to buffer %d: (%d %d) %s .\n",
  1505. __FILE__, __LINE__, bo_gem->gem_handle, (int)offset,
  1506. (int)size, strerror(errno));
  1507. return ret;
  1508. }
  1509. if (ret == -EOPNOTSUPP)
  1510. mmap_write(bo, offset, size, data);
  1511. return 0;
  1512. }
  1513. static int
  1514. drm_intel_gem_get_pipe_from_crtc_id(drm_intel_bufmgr *bufmgr, int crtc_id)
  1515. {
  1516. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
  1517. struct drm_i915_get_pipe_from_crtc_id get_pipe_from_crtc_id;
  1518. int ret;
  1519. memclear(get_pipe_from_crtc_id);
  1520. get_pipe_from_crtc_id.crtc_id = crtc_id;
  1521. ret = drmIoctl(bufmgr_gem->fd,
  1522. DRM_IOCTL_I915_GET_PIPE_FROM_CRTC_ID,
  1523. &get_pipe_from_crtc_id);
  1524. if (ret != 0) {
  1525. /* We return -1 here to signal that we don't
  1526. * know which pipe is associated with this crtc.
  1527. * This lets the caller know that this information
  1528. * isn't available; using the wrong pipe for
  1529. * vblank waiting can cause the chipset to lock up
  1530. */
  1531. return -1;
  1532. }
  1533. return get_pipe_from_crtc_id.pipe;
  1534. }
  1535. static int
  1536. drm_intel_gem_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
  1537. unsigned long size, void *data)
  1538. {
  1539. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1540. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1541. struct drm_i915_gem_pread pread;
  1542. int ret;
  1543. if (bo_gem->is_userptr)
  1544. return -EINVAL;
  1545. memclear(pread);
  1546. pread.handle = bo_gem->gem_handle;
  1547. pread.offset = offset;
  1548. pread.size = size;
  1549. pread.data_ptr = (uint64_t) (uintptr_t) data;
  1550. ret = drmIoctl(bufmgr_gem->fd,
  1551. DRM_IOCTL_I915_GEM_PREAD,
  1552. &pread);
  1553. if (ret)
  1554. ret = -errno;
  1555. if (ret != 0 && ret != -EOPNOTSUPP) {
  1556. DBG("%s:%d: Error reading data from buffer %d: (%d %d) %s .\n",
  1557. __FILE__, __LINE__, bo_gem->gem_handle, (int)offset,
  1558. (int)size, strerror(errno));
  1559. return ret;
  1560. }
  1561. if (ret == -EOPNOTSUPP)
  1562. mmap_read(bo, offset, size, data);
  1563. return 0;
  1564. }
  1565. /** Waits for all GPU rendering with the object to have completed. */
  1566. static void
  1567. drm_intel_gem_bo_wait_rendering(drm_intel_bo *bo)
  1568. {
  1569. drm_intel_gem_bo_start_gtt_access(bo, 1);
  1570. }
  1571. /**
  1572. * Waits on a BO for the given amount of time.
  1573. *
  1574. * @bo: buffer object to wait for
  1575. * @timeout_ns: amount of time to wait in nanoseconds.
  1576. * If value is less than 0, an infinite wait will occur.
  1577. *
  1578. * Returns 0 if the wait was successful ie. the last batch referencing the
  1579. * object has completed within the allotted time. Otherwise some negative return
  1580. * value describes the error. Of particular interest is -ETIME when the wait has
  1581. * failed to yield the desired result.
  1582. *
  1583. * Similar to drm_intel_gem_bo_wait_rendering except a timeout parameter allows
  1584. * the operation to give up after a certain amount of time. Another subtle
  1585. * difference is the internal locking semantics are different (this variant does
  1586. * not hold the lock for the duration of the wait). This makes the wait subject
  1587. * to a larger userspace race window.
  1588. *
  1589. * The implementation shall wait until the object is no longer actively
  1590. * referenced within a batch buffer at the time of the call. The wait will
  1591. * not guarantee that the buffer is re-issued via another thread, or an flinked
  1592. * handle. Userspace must make sure this race does not occur if such precision
  1593. * is important.
  1594. *
  1595. * Note that some kernels have broken the inifite wait for negative values
  1596. * promise, upgrade to latest stable kernels if this is the case.
  1597. */
  1598. drm_public int
  1599. drm_intel_gem_bo_wait(drm_intel_bo *bo, int64_t timeout_ns)
  1600. {
  1601. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1602. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1603. struct drm_i915_gem_wait wait;
  1604. int ret;
  1605. if (!bufmgr_gem->has_wait_timeout) {
  1606. DBG("%s:%d: Timed wait is not supported. Falling back to "
  1607. "infinite wait\n", __FILE__, __LINE__);
  1608. if (timeout_ns) {
  1609. drm_intel_gem_bo_wait_rendering(bo);
  1610. return 0;
  1611. } else {
  1612. return drm_intel_gem_bo_busy(bo) ? -ETIME : 0;
  1613. }
  1614. }
  1615. memclear(wait);
  1616. wait.bo_handle = bo_gem->gem_handle;
  1617. wait.timeout_ns = timeout_ns;
  1618. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_WAIT, &wait);
  1619. if (ret == -1)
  1620. return -errno;
  1621. return ret;
  1622. }
  1623. /**
  1624. * Sets the object to the GTT read and possibly write domain, used by the X
  1625. * 2D driver in the absence of kernel support to do drm_intel_gem_bo_map_gtt().
  1626. *
  1627. * In combination with drm_intel_gem_bo_pin() and manual fence management, we
  1628. * can do tiled pixmaps this way.
  1629. */
  1630. drm_public void
  1631. drm_intel_gem_bo_start_gtt_access(drm_intel_bo *bo, int write_enable)
  1632. {
  1633. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1634. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1635. struct drm_i915_gem_set_domain set_domain;
  1636. int ret;
  1637. memclear(set_domain);
  1638. set_domain.handle = bo_gem->gem_handle;
  1639. set_domain.read_domains = I915_GEM_DOMAIN_GTT;
  1640. set_domain.write_domain = write_enable ? I915_GEM_DOMAIN_GTT : 0;
  1641. ret = drmIoctl(bufmgr_gem->fd,
  1642. DRM_IOCTL_I915_GEM_SET_DOMAIN,
  1643. &set_domain);
  1644. if (ret != 0) {
  1645. DBG("%s:%d: Error setting memory domains %d (%08x %08x): %s .\n",
  1646. __FILE__, __LINE__, bo_gem->gem_handle,
  1647. set_domain.read_domains, set_domain.write_domain,
  1648. strerror(errno));
  1649. }
  1650. }
  1651. static void
  1652. drm_intel_bufmgr_gem_destroy(drm_intel_bufmgr *bufmgr)
  1653. {
  1654. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
  1655. int i, ret;
  1656. free(bufmgr_gem->exec2_objects);
  1657. free(bufmgr_gem->exec_bos);
  1658. pthread_mutex_destroy(&bufmgr_gem->lock);
  1659. /* Free any cached buffer objects we were going to reuse */
  1660. for (i = 0; i < bufmgr_gem->num_buckets; i++) {
  1661. struct drm_intel_gem_bo_bucket *bucket =
  1662. &bufmgr_gem->cache_bucket[i];
  1663. drm_intel_bo_gem *bo_gem;
  1664. while (!DRMLISTEMPTY(&bucket->head)) {
  1665. bo_gem = DRMLISTENTRY(drm_intel_bo_gem,
  1666. bucket->head.next, head);
  1667. DRMLISTDEL(&bo_gem->head);
  1668. drm_intel_gem_bo_free(&bo_gem->bo);
  1669. }
  1670. }
  1671. /* Release userptr bo kept hanging around for optimisation. */
  1672. if (bufmgr_gem->userptr_active.ptr) {
  1673. ret = drmCloseBufferHandle(bufmgr_gem->fd,
  1674. bufmgr_gem->userptr_active.handle);
  1675. free(bufmgr_gem->userptr_active.ptr);
  1676. if (ret)
  1677. fprintf(stderr,
  1678. "Failed to release test userptr object! (%d) "
  1679. "i915 kernel driver may not be sane!\n", errno);
  1680. }
  1681. free(bufmgr);
  1682. }
  1683. /**
  1684. * Adds the target buffer to the validation list and adds the relocation
  1685. * to the reloc_buffer's relocation list.
  1686. *
  1687. * The relocation entry at the given offset must already contain the
  1688. * precomputed relocation value, because the kernel will optimize out
  1689. * the relocation entry write when the buffer hasn't moved from the
  1690. * last known offset in target_bo.
  1691. */
  1692. static int
  1693. do_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
  1694. drm_intel_bo *target_bo, uint32_t target_offset,
  1695. uint32_t read_domains, uint32_t write_domain,
  1696. bool need_fence)
  1697. {
  1698. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1699. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1700. drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo;
  1701. bool fenced_command;
  1702. if (bo_gem->has_error)
  1703. return -ENOMEM;
  1704. if (target_bo_gem->has_error) {
  1705. bo_gem->has_error = true;
  1706. return -ENOMEM;
  1707. }
  1708. /* We never use HW fences for rendering on 965+ */
  1709. if (bufmgr_gem->gen >= 4)
  1710. need_fence = false;
  1711. fenced_command = need_fence;
  1712. if (target_bo_gem->tiling_mode == I915_TILING_NONE)
  1713. need_fence = false;
  1714. /* Create a new relocation list if needed */
  1715. if (bo_gem->relocs == NULL && drm_intel_setup_reloc_list(bo))
  1716. return -ENOMEM;
  1717. /* Check overflow */
  1718. assert(bo_gem->reloc_count < bufmgr_gem->max_relocs);
  1719. /* Check args */
  1720. assert(offset <= bo->size - 4);
  1721. assert((write_domain & (write_domain - 1)) == 0);
  1722. /* An object needing a fence is a tiled buffer, so it won't have
  1723. * relocs to other buffers.
  1724. */
  1725. if (need_fence) {
  1726. assert(target_bo_gem->reloc_count == 0);
  1727. target_bo_gem->reloc_tree_fences = 1;
  1728. }
  1729. /* Make sure that we're not adding a reloc to something whose size has
  1730. * already been accounted for.
  1731. */
  1732. assert(!bo_gem->used_as_reloc_target);
  1733. if (target_bo_gem != bo_gem) {
  1734. target_bo_gem->used_as_reloc_target = true;
  1735. bo_gem->reloc_tree_size += target_bo_gem->reloc_tree_size;
  1736. bo_gem->reloc_tree_fences += target_bo_gem->reloc_tree_fences;
  1737. }
  1738. bo_gem->reloc_target_info[bo_gem->reloc_count].bo = target_bo;
  1739. if (target_bo != bo)
  1740. drm_intel_gem_bo_reference(target_bo);
  1741. if (fenced_command)
  1742. bo_gem->reloc_target_info[bo_gem->reloc_count].flags =
  1743. DRM_INTEL_RELOC_FENCE;
  1744. else
  1745. bo_gem->reloc_target_info[bo_gem->reloc_count].flags = 0;
  1746. bo_gem->relocs[bo_gem->reloc_count].offset = offset;
  1747. bo_gem->relocs[bo_gem->reloc_count].delta = target_offset;
  1748. bo_gem->relocs[bo_gem->reloc_count].target_handle =
  1749. target_bo_gem->gem_handle;
  1750. bo_gem->relocs[bo_gem->reloc_count].read_domains = read_domains;
  1751. bo_gem->relocs[bo_gem->reloc_count].write_domain = write_domain;
  1752. bo_gem->relocs[bo_gem->reloc_count].presumed_offset = target_bo->offset64;
  1753. bo_gem->reloc_count++;
  1754. return 0;
  1755. }
  1756. static void
  1757. drm_intel_gem_bo_use_48b_address_range(drm_intel_bo *bo, uint32_t enable)
  1758. {
  1759. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1760. if (enable)
  1761. bo_gem->kflags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
  1762. else
  1763. bo_gem->kflags &= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
  1764. }
  1765. static int
  1766. drm_intel_gem_bo_add_softpin_target(drm_intel_bo *bo, drm_intel_bo *target_bo)
  1767. {
  1768. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1769. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1770. drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo;
  1771. if (bo_gem->has_error)
  1772. return -ENOMEM;
  1773. if (target_bo_gem->has_error) {
  1774. bo_gem->has_error = true;
  1775. return -ENOMEM;
  1776. }
  1777. if (!(target_bo_gem->kflags & EXEC_OBJECT_PINNED))
  1778. return -EINVAL;
  1779. if (target_bo_gem == bo_gem)
  1780. return -EINVAL;
  1781. if (bo_gem->softpin_target_count == bo_gem->softpin_target_size) {
  1782. int new_size = bo_gem->softpin_target_size * 2;
  1783. if (new_size == 0)
  1784. new_size = bufmgr_gem->max_relocs;
  1785. bo_gem->softpin_target = realloc(bo_gem->softpin_target, new_size *
  1786. sizeof(drm_intel_bo *));
  1787. if (!bo_gem->softpin_target)
  1788. return -ENOMEM;
  1789. bo_gem->softpin_target_size = new_size;
  1790. }
  1791. bo_gem->softpin_target[bo_gem->softpin_target_count] = target_bo;
  1792. drm_intel_gem_bo_reference(target_bo);
  1793. bo_gem->softpin_target_count++;
  1794. return 0;
  1795. }
  1796. static int
  1797. drm_intel_gem_bo_emit_reloc(drm_intel_bo *bo, uint32_t offset,
  1798. drm_intel_bo *target_bo, uint32_t target_offset,
  1799. uint32_t read_domains, uint32_t write_domain)
  1800. {
  1801. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
  1802. drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *)target_bo;
  1803. if (target_bo_gem->kflags & EXEC_OBJECT_PINNED)
  1804. return drm_intel_gem_bo_add_softpin_target(bo, target_bo);
  1805. else
  1806. return do_bo_emit_reloc(bo, offset, target_bo, target_offset,
  1807. read_domains, write_domain,
  1808. !bufmgr_gem->fenced_relocs);
  1809. }
  1810. static int
  1811. drm_intel_gem_bo_emit_reloc_fence(drm_intel_bo *bo, uint32_t offset,
  1812. drm_intel_bo *target_bo,
  1813. uint32_t target_offset,
  1814. uint32_t read_domains, uint32_t write_domain)
  1815. {
  1816. return do_bo_emit_reloc(bo, offset, target_bo, target_offset,
  1817. read_domains, write_domain, true);
  1818. }
  1819. drm_public int
  1820. drm_intel_gem_bo_get_reloc_count(drm_intel_bo *bo)
  1821. {
  1822. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1823. return bo_gem->reloc_count;
  1824. }
  1825. /**
  1826. * Removes existing relocation entries in the BO after "start".
  1827. *
  1828. * This allows a user to avoid a two-step process for state setup with
  1829. * counting up all the buffer objects and doing a
  1830. * drm_intel_bufmgr_check_aperture_space() before emitting any of the
  1831. * relocations for the state setup. Instead, save the state of the
  1832. * batchbuffer including drm_intel_gem_get_reloc_count(), emit all the
  1833. * state, and then check if it still fits in the aperture.
  1834. *
  1835. * Any further drm_intel_bufmgr_check_aperture_space() queries
  1836. * involving this buffer in the tree are undefined after this call.
  1837. *
  1838. * This also removes all softpinned targets being referenced by the BO.
  1839. */
  1840. drm_public void
  1841. drm_intel_gem_bo_clear_relocs(drm_intel_bo *bo, int start)
  1842. {
  1843. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  1844. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  1845. int i;
  1846. struct timespec time;
  1847. clock_gettime(CLOCK_MONOTONIC, &time);
  1848. assert(bo_gem->reloc_count >= start);
  1849. /* Unreference the cleared target buffers */
  1850. pthread_mutex_lock(&bufmgr_gem->lock);
  1851. for (i = start; i < bo_gem->reloc_count; i++) {
  1852. drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) bo_gem->reloc_target_info[i].bo;
  1853. if (&target_bo_gem->bo != bo) {
  1854. bo_gem->reloc_tree_fences -= target_bo_gem->reloc_tree_fences;
  1855. drm_intel_gem_bo_unreference_locked_timed(&target_bo_gem->bo,
  1856. time.tv_sec);
  1857. }
  1858. }
  1859. bo_gem->reloc_count = start;
  1860. for (i = 0; i < bo_gem->softpin_target_count; i++) {
  1861. drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) bo_gem->softpin_target[i];
  1862. drm_intel_gem_bo_unreference_locked_timed(&target_bo_gem->bo, time.tv_sec);
  1863. }
  1864. bo_gem->softpin_target_count = 0;
  1865. pthread_mutex_unlock(&bufmgr_gem->lock);
  1866. }
  1867. /**
  1868. * Walk the tree of relocations rooted at BO and accumulate the list of
  1869. * validations to be performed and update the relocation buffers with
  1870. * index values into the validation list.
  1871. */
  1872. static void
  1873. drm_intel_gem_bo_process_reloc2(drm_intel_bo *bo)
  1874. {
  1875. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
  1876. int i;
  1877. if (bo_gem->relocs == NULL && bo_gem->softpin_target == NULL)
  1878. return;
  1879. for (i = 0; i < bo_gem->reloc_count; i++) {
  1880. drm_intel_bo *target_bo = bo_gem->reloc_target_info[i].bo;
  1881. int need_fence;
  1882. if (target_bo == bo)
  1883. continue;
  1884. drm_intel_gem_bo_mark_mmaps_incoherent(bo);
  1885. /* Continue walking the tree depth-first. */
  1886. drm_intel_gem_bo_process_reloc2(target_bo);
  1887. need_fence = (bo_gem->reloc_target_info[i].flags &
  1888. DRM_INTEL_RELOC_FENCE);
  1889. /* Add the target to the validate list */
  1890. drm_intel_add_validate_buffer2(target_bo, need_fence);
  1891. }
  1892. for (i = 0; i < bo_gem->softpin_target_count; i++) {
  1893. drm_intel_bo *target_bo = bo_gem->softpin_target[i];
  1894. if (target_bo == bo)
  1895. continue;
  1896. drm_intel_gem_bo_mark_mmaps_incoherent(bo);
  1897. drm_intel_gem_bo_process_reloc2(target_bo);
  1898. drm_intel_add_validate_buffer2(target_bo, false);
  1899. }
  1900. }
  1901. static void
  1902. drm_intel_update_buffer_offsets2 (drm_intel_bufmgr_gem *bufmgr_gem)
  1903. {
  1904. int i;
  1905. for (i = 0; i < bufmgr_gem->exec_count; i++) {
  1906. drm_intel_bo *bo = bufmgr_gem->exec_bos[i];
  1907. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *)bo;
  1908. /* Update the buffer offset */
  1909. if (bufmgr_gem->exec2_objects[i].offset != bo->offset64) {
  1910. /* If we're seeing softpinned object here it means that the kernel
  1911. * has relocated our object... Indicating a programming error
  1912. */
  1913. assert(!(bo_gem->kflags & EXEC_OBJECT_PINNED));
  1914. DBG("BO %d (%s) migrated: 0x%08x %08x -> 0x%08x %08x\n",
  1915. bo_gem->gem_handle, bo_gem->name,
  1916. upper_32_bits(bo->offset64),
  1917. lower_32_bits(bo->offset64),
  1918. upper_32_bits(bufmgr_gem->exec2_objects[i].offset),
  1919. lower_32_bits(bufmgr_gem->exec2_objects[i].offset));
  1920. bo->offset64 = bufmgr_gem->exec2_objects[i].offset;
  1921. bo->offset = bufmgr_gem->exec2_objects[i].offset;
  1922. }
  1923. }
  1924. }
  1925. drm_public void
  1926. drm_intel_gem_bo_aub_dump_bmp(drm_intel_bo *bo,
  1927. int x1, int y1, int width, int height,
  1928. enum aub_dump_bmp_format format,
  1929. int pitch, int offset)
  1930. {
  1931. }
  1932. static int
  1933. do_exec2(drm_intel_bo *bo, int used, drm_intel_context *ctx,
  1934. drm_clip_rect_t *cliprects, int num_cliprects, int DR4,
  1935. int in_fence, int *out_fence,
  1936. unsigned int flags)
  1937. {
  1938. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bo->bufmgr;
  1939. struct drm_i915_gem_execbuffer2 execbuf;
  1940. int ret = 0;
  1941. int i;
  1942. if (to_bo_gem(bo)->has_error)
  1943. return -ENOMEM;
  1944. switch (flags & 0x7) {
  1945. default:
  1946. return -EINVAL;
  1947. case I915_EXEC_BLT:
  1948. if (!bufmgr_gem->has_blt)
  1949. return -EINVAL;
  1950. break;
  1951. case I915_EXEC_BSD:
  1952. if (!bufmgr_gem->has_bsd)
  1953. return -EINVAL;
  1954. break;
  1955. case I915_EXEC_VEBOX:
  1956. if (!bufmgr_gem->has_vebox)
  1957. return -EINVAL;
  1958. break;
  1959. case I915_EXEC_RENDER:
  1960. case I915_EXEC_DEFAULT:
  1961. break;
  1962. }
  1963. pthread_mutex_lock(&bufmgr_gem->lock);
  1964. /* Update indices and set up the validate list. */
  1965. drm_intel_gem_bo_process_reloc2(bo);
  1966. /* Add the batch buffer to the validation list. There are no relocations
  1967. * pointing to it.
  1968. */
  1969. drm_intel_add_validate_buffer2(bo, 0);
  1970. memclear(execbuf);
  1971. execbuf.buffers_ptr = (uintptr_t)bufmgr_gem->exec2_objects;
  1972. execbuf.buffer_count = bufmgr_gem->exec_count;
  1973. execbuf.batch_start_offset = 0;
  1974. execbuf.batch_len = used;
  1975. execbuf.cliprects_ptr = (uintptr_t)cliprects;
  1976. execbuf.num_cliprects = num_cliprects;
  1977. execbuf.DR1 = 0;
  1978. execbuf.DR4 = DR4;
  1979. execbuf.flags = flags;
  1980. if (ctx == NULL)
  1981. i915_execbuffer2_set_context_id(execbuf, 0);
  1982. else
  1983. i915_execbuffer2_set_context_id(execbuf, ctx->ctx_id);
  1984. execbuf.rsvd2 = 0;
  1985. if (in_fence != -1) {
  1986. execbuf.rsvd2 = in_fence;
  1987. execbuf.flags |= I915_EXEC_FENCE_IN;
  1988. }
  1989. if (out_fence != NULL) {
  1990. *out_fence = -1;
  1991. execbuf.flags |= I915_EXEC_FENCE_OUT;
  1992. }
  1993. if (bufmgr_gem->no_exec)
  1994. goto skip_execution;
  1995. ret = drmIoctl(bufmgr_gem->fd,
  1996. DRM_IOCTL_I915_GEM_EXECBUFFER2_WR,
  1997. &execbuf);
  1998. if (ret != 0) {
  1999. ret = -errno;
  2000. if (ret == -ENOSPC) {
  2001. DBG("Execbuffer fails to pin. "
  2002. "Estimate: %u. Actual: %u. Available: %u\n",
  2003. drm_intel_gem_estimate_batch_space(bufmgr_gem->exec_bos,
  2004. bufmgr_gem->exec_count),
  2005. drm_intel_gem_compute_batch_space(bufmgr_gem->exec_bos,
  2006. bufmgr_gem->exec_count),
  2007. (unsigned int) bufmgr_gem->gtt_size);
  2008. }
  2009. }
  2010. drm_intel_update_buffer_offsets2(bufmgr_gem);
  2011. if (ret == 0 && out_fence != NULL)
  2012. *out_fence = execbuf.rsvd2 >> 32;
  2013. skip_execution:
  2014. if (bufmgr_gem->bufmgr.debug)
  2015. drm_intel_gem_dump_validation_list(bufmgr_gem);
  2016. for (i = 0; i < bufmgr_gem->exec_count; i++) {
  2017. drm_intel_bo_gem *bo_gem = to_bo_gem(bufmgr_gem->exec_bos[i]);
  2018. bo_gem->idle = false;
  2019. /* Disconnect the buffer from the validate list */
  2020. bo_gem->validate_index = -1;
  2021. bufmgr_gem->exec_bos[i] = NULL;
  2022. }
  2023. bufmgr_gem->exec_count = 0;
  2024. pthread_mutex_unlock(&bufmgr_gem->lock);
  2025. return ret;
  2026. }
  2027. static int
  2028. drm_intel_gem_bo_exec2(drm_intel_bo *bo, int used,
  2029. drm_clip_rect_t *cliprects, int num_cliprects,
  2030. int DR4)
  2031. {
  2032. return do_exec2(bo, used, NULL, cliprects, num_cliprects, DR4,
  2033. -1, NULL, I915_EXEC_RENDER);
  2034. }
  2035. static int
  2036. drm_intel_gem_bo_mrb_exec2(drm_intel_bo *bo, int used,
  2037. drm_clip_rect_t *cliprects, int num_cliprects, int DR4,
  2038. unsigned int flags)
  2039. {
  2040. return do_exec2(bo, used, NULL, cliprects, num_cliprects, DR4,
  2041. -1, NULL, flags);
  2042. }
  2043. drm_public int
  2044. drm_intel_gem_bo_context_exec(drm_intel_bo *bo, drm_intel_context *ctx,
  2045. int used, unsigned int flags)
  2046. {
  2047. return do_exec2(bo, used, ctx, NULL, 0, 0, -1, NULL, flags);
  2048. }
  2049. drm_public int
  2050. drm_intel_gem_bo_fence_exec(drm_intel_bo *bo,
  2051. drm_intel_context *ctx,
  2052. int used,
  2053. int in_fence,
  2054. int *out_fence,
  2055. unsigned int flags)
  2056. {
  2057. return do_exec2(bo, used, ctx, NULL, 0, 0, in_fence, out_fence, flags);
  2058. }
  2059. static int
  2060. drm_intel_gem_bo_pin(drm_intel_bo *bo, uint32_t alignment)
  2061. {
  2062. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  2063. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2064. struct drm_i915_gem_pin pin;
  2065. int ret;
  2066. memclear(pin);
  2067. pin.handle = bo_gem->gem_handle;
  2068. pin.alignment = alignment;
  2069. ret = drmIoctl(bufmgr_gem->fd,
  2070. DRM_IOCTL_I915_GEM_PIN,
  2071. &pin);
  2072. if (ret != 0)
  2073. return -errno;
  2074. bo->offset64 = pin.offset;
  2075. bo->offset = pin.offset;
  2076. return 0;
  2077. }
  2078. static int
  2079. drm_intel_gem_bo_unpin(drm_intel_bo *bo)
  2080. {
  2081. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  2082. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2083. struct drm_i915_gem_unpin unpin;
  2084. int ret;
  2085. memclear(unpin);
  2086. unpin.handle = bo_gem->gem_handle;
  2087. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_UNPIN, &unpin);
  2088. if (ret != 0)
  2089. return -errno;
  2090. return 0;
  2091. }
  2092. static int
  2093. drm_intel_gem_bo_set_tiling_internal(drm_intel_bo *bo,
  2094. uint32_t tiling_mode,
  2095. uint32_t stride)
  2096. {
  2097. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  2098. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2099. struct drm_i915_gem_set_tiling set_tiling;
  2100. int ret;
  2101. if (bo_gem->global_name == 0 &&
  2102. tiling_mode == bo_gem->tiling_mode &&
  2103. stride == bo_gem->stride)
  2104. return 0;
  2105. memset(&set_tiling, 0, sizeof(set_tiling));
  2106. do {
  2107. /* set_tiling is slightly broken and overwrites the
  2108. * input on the error path, so we have to open code
  2109. * rmIoctl.
  2110. */
  2111. set_tiling.handle = bo_gem->gem_handle;
  2112. set_tiling.tiling_mode = tiling_mode;
  2113. set_tiling.stride = stride;
  2114. ret = ioctl(bufmgr_gem->fd,
  2115. DRM_IOCTL_I915_GEM_SET_TILING,
  2116. &set_tiling);
  2117. } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
  2118. if (ret == -1)
  2119. return -errno;
  2120. bo_gem->tiling_mode = set_tiling.tiling_mode;
  2121. bo_gem->swizzle_mode = set_tiling.swizzle_mode;
  2122. bo_gem->stride = set_tiling.stride;
  2123. return 0;
  2124. }
  2125. static int
  2126. drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
  2127. uint32_t stride)
  2128. {
  2129. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  2130. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2131. int ret;
  2132. /* Tiling with userptr surfaces is not supported
  2133. * on all hardware so refuse it for time being.
  2134. */
  2135. if (bo_gem->is_userptr)
  2136. return -EINVAL;
  2137. /* Linear buffers have no stride. By ensuring that we only ever use
  2138. * stride 0 with linear buffers, we simplify our code.
  2139. */
  2140. if (*tiling_mode == I915_TILING_NONE)
  2141. stride = 0;
  2142. ret = drm_intel_gem_bo_set_tiling_internal(bo, *tiling_mode, stride);
  2143. if (ret == 0)
  2144. drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem, 0);
  2145. *tiling_mode = bo_gem->tiling_mode;
  2146. return ret;
  2147. }
  2148. static int
  2149. drm_intel_gem_bo_get_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
  2150. uint32_t * swizzle_mode)
  2151. {
  2152. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2153. *tiling_mode = bo_gem->tiling_mode;
  2154. *swizzle_mode = bo_gem->swizzle_mode;
  2155. return 0;
  2156. }
  2157. static int
  2158. drm_intel_gem_bo_set_softpin_offset(drm_intel_bo *bo, uint64_t offset)
  2159. {
  2160. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2161. bo->offset64 = offset;
  2162. bo->offset = offset;
  2163. bo_gem->kflags |= EXEC_OBJECT_PINNED;
  2164. return 0;
  2165. }
  2166. drm_public drm_intel_bo *
  2167. drm_intel_bo_gem_create_from_prime(drm_intel_bufmgr *bufmgr, int prime_fd, int size)
  2168. {
  2169. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
  2170. int ret;
  2171. uint32_t handle;
  2172. drm_intel_bo_gem *bo_gem;
  2173. pthread_mutex_lock(&bufmgr_gem->lock);
  2174. ret = drmPrimeFDToHandle(bufmgr_gem->fd, prime_fd, &handle);
  2175. if (ret) {
  2176. DBG("create_from_prime: failed to obtain handle from fd: %s\n", strerror(errno));
  2177. pthread_mutex_unlock(&bufmgr_gem->lock);
  2178. return NULL;
  2179. }
  2180. /*
  2181. * See if the kernel has already returned this buffer to us. Just as
  2182. * for named buffers, we must not create two bo's pointing at the same
  2183. * kernel object
  2184. */
  2185. HASH_FIND(handle_hh, bufmgr_gem->handle_table,
  2186. &handle, sizeof(handle), bo_gem);
  2187. if (bo_gem) {
  2188. drm_intel_gem_bo_reference(&bo_gem->bo);
  2189. goto out;
  2190. }
  2191. bo_gem = calloc(1, sizeof(*bo_gem));
  2192. if (!bo_gem)
  2193. goto out;
  2194. atomic_set(&bo_gem->refcount, 1);
  2195. DRMINITLISTHEAD(&bo_gem->vma_list);
  2196. /* Determine size of bo. The fd-to-handle ioctl really should
  2197. * return the size, but it doesn't. If we have kernel 3.12 or
  2198. * later, we can lseek on the prime fd to get the size. Older
  2199. * kernels will just fail, in which case we fall back to the
  2200. * provided (estimated or guess size). */
  2201. ret = lseek(prime_fd, 0, SEEK_END);
  2202. if (ret != -1)
  2203. bo_gem->bo.size = ret;
  2204. else
  2205. bo_gem->bo.size = size;
  2206. bo_gem->bo.handle = handle;
  2207. bo_gem->bo.bufmgr = bufmgr;
  2208. bo_gem->gem_handle = handle;
  2209. HASH_ADD(handle_hh, bufmgr_gem->handle_table,
  2210. gem_handle, sizeof(bo_gem->gem_handle), bo_gem);
  2211. bo_gem->name = "prime";
  2212. bo_gem->validate_index = -1;
  2213. bo_gem->reloc_tree_fences = 0;
  2214. bo_gem->used_as_reloc_target = false;
  2215. bo_gem->has_error = false;
  2216. bo_gem->reusable = false;
  2217. ret = get_tiling_mode(bufmgr_gem, handle,
  2218. &bo_gem->tiling_mode, &bo_gem->swizzle_mode);
  2219. if (ret)
  2220. goto err;
  2221. /* XXX stride is unknown */
  2222. drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem, 0);
  2223. out:
  2224. pthread_mutex_unlock(&bufmgr_gem->lock);
  2225. return &bo_gem->bo;
  2226. err:
  2227. drm_intel_gem_bo_free(&bo_gem->bo);
  2228. pthread_mutex_unlock(&bufmgr_gem->lock);
  2229. return NULL;
  2230. }
  2231. drm_public int
  2232. drm_intel_bo_gem_export_to_prime(drm_intel_bo *bo, int *prime_fd)
  2233. {
  2234. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  2235. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2236. if (drmPrimeHandleToFD(bufmgr_gem->fd, bo_gem->gem_handle,
  2237. DRM_CLOEXEC | DRM_RDWR, prime_fd) != 0)
  2238. return -errno;
  2239. bo_gem->reusable = false;
  2240. return 0;
  2241. }
  2242. static int
  2243. drm_intel_gem_bo_flink(drm_intel_bo *bo, uint32_t * name)
  2244. {
  2245. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  2246. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2247. if (!bo_gem->global_name) {
  2248. struct drm_gem_flink flink;
  2249. memclear(flink);
  2250. flink.handle = bo_gem->gem_handle;
  2251. if (drmIoctl(bufmgr_gem->fd, DRM_IOCTL_GEM_FLINK, &flink))
  2252. return -errno;
  2253. pthread_mutex_lock(&bufmgr_gem->lock);
  2254. if (!bo_gem->global_name) {
  2255. bo_gem->global_name = flink.name;
  2256. bo_gem->reusable = false;
  2257. HASH_ADD(name_hh, bufmgr_gem->name_table,
  2258. global_name, sizeof(bo_gem->global_name),
  2259. bo_gem);
  2260. }
  2261. pthread_mutex_unlock(&bufmgr_gem->lock);
  2262. }
  2263. *name = bo_gem->global_name;
  2264. return 0;
  2265. }
  2266. /**
  2267. * Enables unlimited caching of buffer objects for reuse.
  2268. *
  2269. * This is potentially very memory expensive, as the cache at each bucket
  2270. * size is only bounded by how many buffers of that size we've managed to have
  2271. * in flight at once.
  2272. */
  2273. drm_public void
  2274. drm_intel_bufmgr_gem_enable_reuse(drm_intel_bufmgr *bufmgr)
  2275. {
  2276. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
  2277. bufmgr_gem->bo_reuse = true;
  2278. }
  2279. /**
  2280. * Disables implicit synchronisation before executing the bo
  2281. *
  2282. * This will cause rendering corruption unless you correctly manage explicit
  2283. * fences for all rendering involving this buffer - including use by others.
  2284. * Disabling the implicit serialisation is only required if that serialisation
  2285. * is too coarse (for example, you have split the buffer into many
  2286. * non-overlapping regions and are sharing the whole buffer between concurrent
  2287. * independent command streams).
  2288. *
  2289. * Note the kernel must advertise support via I915_PARAM_HAS_EXEC_ASYNC,
  2290. * which can be checked using drm_intel_bufmgr_can_disable_implicit_sync,
  2291. * or subsequent execbufs involving the bo will generate EINVAL.
  2292. */
  2293. drm_public void
  2294. drm_intel_gem_bo_disable_implicit_sync(drm_intel_bo *bo)
  2295. {
  2296. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2297. bo_gem->kflags |= EXEC_OBJECT_ASYNC;
  2298. }
  2299. /**
  2300. * Enables implicit synchronisation before executing the bo
  2301. *
  2302. * This is the default behaviour of the kernel, to wait upon prior writes
  2303. * completing on the object before rendering with it, or to wait for prior
  2304. * reads to complete before writing into the object.
  2305. * drm_intel_gem_bo_disable_implicit_sync() can stop this behaviour, telling
  2306. * the kernel never to insert a stall before using the object. Then this
  2307. * function can be used to restore the implicit sync before subsequent
  2308. * rendering.
  2309. */
  2310. drm_public void
  2311. drm_intel_gem_bo_enable_implicit_sync(drm_intel_bo *bo)
  2312. {
  2313. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2314. bo_gem->kflags &= ~EXEC_OBJECT_ASYNC;
  2315. }
  2316. /**
  2317. * Query whether the kernel supports disabling of its implicit synchronisation
  2318. * before execbuf. See drm_intel_gem_bo_disable_implicit_sync()
  2319. */
  2320. drm_public int
  2321. drm_intel_bufmgr_gem_can_disable_implicit_sync(drm_intel_bufmgr *bufmgr)
  2322. {
  2323. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
  2324. return bufmgr_gem->has_exec_async;
  2325. }
  2326. /**
  2327. * Enable use of fenced reloc type.
  2328. *
  2329. * New code should enable this to avoid unnecessary fence register
  2330. * allocation. If this option is not enabled, all relocs will have fence
  2331. * register allocated.
  2332. */
  2333. drm_public void
  2334. drm_intel_bufmgr_gem_enable_fenced_relocs(drm_intel_bufmgr *bufmgr)
  2335. {
  2336. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
  2337. bufmgr_gem->fenced_relocs = true;
  2338. }
  2339. /**
  2340. * Return the additional aperture space required by the tree of buffer objects
  2341. * rooted at bo.
  2342. */
  2343. static int
  2344. drm_intel_gem_bo_get_aperture_space(drm_intel_bo *bo)
  2345. {
  2346. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2347. int i;
  2348. int total = 0;
  2349. if (bo == NULL || bo_gem->included_in_check_aperture)
  2350. return 0;
  2351. total += bo->size;
  2352. bo_gem->included_in_check_aperture = true;
  2353. for (i = 0; i < bo_gem->reloc_count; i++)
  2354. total +=
  2355. drm_intel_gem_bo_get_aperture_space(bo_gem->
  2356. reloc_target_info[i].bo);
  2357. return total;
  2358. }
  2359. /**
  2360. * Count the number of buffers in this list that need a fence reg
  2361. *
  2362. * If the count is greater than the number of available regs, we'll have
  2363. * to ask the caller to resubmit a batch with fewer tiled buffers.
  2364. *
  2365. * This function over-counts if the same buffer is used multiple times.
  2366. */
  2367. static unsigned int
  2368. drm_intel_gem_total_fences(drm_intel_bo ** bo_array, int count)
  2369. {
  2370. int i;
  2371. unsigned int total = 0;
  2372. for (i = 0; i < count; i++) {
  2373. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i];
  2374. if (bo_gem == NULL)
  2375. continue;
  2376. total += bo_gem->reloc_tree_fences;
  2377. }
  2378. return total;
  2379. }
  2380. /**
  2381. * Clear the flag set by drm_intel_gem_bo_get_aperture_space() so we're ready
  2382. * for the next drm_intel_bufmgr_check_aperture_space() call.
  2383. */
  2384. static void
  2385. drm_intel_gem_bo_clear_aperture_space_flag(drm_intel_bo *bo)
  2386. {
  2387. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2388. int i;
  2389. if (bo == NULL || !bo_gem->included_in_check_aperture)
  2390. return;
  2391. bo_gem->included_in_check_aperture = false;
  2392. for (i = 0; i < bo_gem->reloc_count; i++)
  2393. drm_intel_gem_bo_clear_aperture_space_flag(bo_gem->
  2394. reloc_target_info[i].bo);
  2395. }
  2396. /**
  2397. * Return a conservative estimate for the amount of aperture required
  2398. * for a collection of buffers. This may double-count some buffers.
  2399. */
  2400. static unsigned int
  2401. drm_intel_gem_estimate_batch_space(drm_intel_bo **bo_array, int count)
  2402. {
  2403. int i;
  2404. unsigned int total = 0;
  2405. for (i = 0; i < count; i++) {
  2406. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo_array[i];
  2407. if (bo_gem != NULL)
  2408. total += bo_gem->reloc_tree_size;
  2409. }
  2410. return total;
  2411. }
  2412. /**
  2413. * Return the amount of aperture needed for a collection of buffers.
  2414. * This avoids double counting any buffers, at the cost of looking
  2415. * at every buffer in the set.
  2416. */
  2417. static unsigned int
  2418. drm_intel_gem_compute_batch_space(drm_intel_bo **bo_array, int count)
  2419. {
  2420. int i;
  2421. unsigned int total = 0;
  2422. for (i = 0; i < count; i++) {
  2423. total += drm_intel_gem_bo_get_aperture_space(bo_array[i]);
  2424. /* For the first buffer object in the array, we get an
  2425. * accurate count back for its reloc_tree size (since nothing
  2426. * had been flagged as being counted yet). We can save that
  2427. * value out as a more conservative reloc_tree_size that
  2428. * avoids double-counting target buffers. Since the first
  2429. * buffer happens to usually be the batch buffer in our
  2430. * callers, this can pull us back from doing the tree
  2431. * walk on every new batch emit.
  2432. */
  2433. if (i == 0) {
  2434. drm_intel_bo_gem *bo_gem =
  2435. (drm_intel_bo_gem *) bo_array[i];
  2436. bo_gem->reloc_tree_size = total;
  2437. }
  2438. }
  2439. for (i = 0; i < count; i++)
  2440. drm_intel_gem_bo_clear_aperture_space_flag(bo_array[i]);
  2441. return total;
  2442. }
  2443. /**
  2444. * Return -1 if the batchbuffer should be flushed before attempting to
  2445. * emit rendering referencing the buffers pointed to by bo_array.
  2446. *
  2447. * This is required because if we try to emit a batchbuffer with relocations
  2448. * to a tree of buffers that won't simultaneously fit in the aperture,
  2449. * the rendering will return an error at a point where the software is not
  2450. * prepared to recover from it.
  2451. *
  2452. * However, we also want to emit the batchbuffer significantly before we reach
  2453. * the limit, as a series of batchbuffers each of which references buffers
  2454. * covering almost all of the aperture means that at each emit we end up
  2455. * waiting to evict a buffer from the last rendering, and we get synchronous
  2456. * performance. By emitting smaller batchbuffers, we eat some CPU overhead to
  2457. * get better parallelism.
  2458. */
  2459. static int
  2460. drm_intel_gem_check_aperture_space(drm_intel_bo **bo_array, int count)
  2461. {
  2462. drm_intel_bufmgr_gem *bufmgr_gem =
  2463. (drm_intel_bufmgr_gem *) bo_array[0]->bufmgr;
  2464. unsigned int total = 0;
  2465. unsigned int threshold = bufmgr_gem->gtt_size * 3 / 4;
  2466. int total_fences;
  2467. /* Check for fence reg constraints if necessary */
  2468. if (bufmgr_gem->available_fences) {
  2469. total_fences = drm_intel_gem_total_fences(bo_array, count);
  2470. if (total_fences > bufmgr_gem->available_fences)
  2471. return -ENOSPC;
  2472. }
  2473. total = drm_intel_gem_estimate_batch_space(bo_array, count);
  2474. if (total > threshold)
  2475. total = drm_intel_gem_compute_batch_space(bo_array, count);
  2476. if (total > threshold) {
  2477. DBG("check_space: overflowed available aperture, "
  2478. "%dkb vs %dkb\n",
  2479. total / 1024, (int)bufmgr_gem->gtt_size / 1024);
  2480. return -ENOSPC;
  2481. } else {
  2482. DBG("drm_check_space: total %dkb vs bufgr %dkb\n", total / 1024,
  2483. (int)bufmgr_gem->gtt_size / 1024);
  2484. return 0;
  2485. }
  2486. }
  2487. /*
  2488. * Disable buffer reuse for objects which are shared with the kernel
  2489. * as scanout buffers
  2490. */
  2491. static int
  2492. drm_intel_gem_bo_disable_reuse(drm_intel_bo *bo)
  2493. {
  2494. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2495. bo_gem->reusable = false;
  2496. return 0;
  2497. }
  2498. static int
  2499. drm_intel_gem_bo_is_reusable(drm_intel_bo *bo)
  2500. {
  2501. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2502. return bo_gem->reusable;
  2503. }
  2504. static int
  2505. _drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
  2506. {
  2507. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2508. int i;
  2509. for (i = 0; i < bo_gem->reloc_count; i++) {
  2510. if (bo_gem->reloc_target_info[i].bo == target_bo)
  2511. return 1;
  2512. if (bo == bo_gem->reloc_target_info[i].bo)
  2513. continue;
  2514. if (_drm_intel_gem_bo_references(bo_gem->reloc_target_info[i].bo,
  2515. target_bo))
  2516. return 1;
  2517. }
  2518. for (i = 0; i< bo_gem->softpin_target_count; i++) {
  2519. if (bo_gem->softpin_target[i] == target_bo)
  2520. return 1;
  2521. if (_drm_intel_gem_bo_references(bo_gem->softpin_target[i], target_bo))
  2522. return 1;
  2523. }
  2524. return 0;
  2525. }
  2526. /** Return true if target_bo is referenced by bo's relocation tree. */
  2527. static int
  2528. drm_intel_gem_bo_references(drm_intel_bo *bo, drm_intel_bo *target_bo)
  2529. {
  2530. drm_intel_bo_gem *target_bo_gem = (drm_intel_bo_gem *) target_bo;
  2531. if (bo == NULL || target_bo == NULL)
  2532. return 0;
  2533. if (target_bo_gem->used_as_reloc_target)
  2534. return _drm_intel_gem_bo_references(bo, target_bo);
  2535. return 0;
  2536. }
  2537. static void
  2538. add_bucket(drm_intel_bufmgr_gem *bufmgr_gem, int size)
  2539. {
  2540. unsigned int i = bufmgr_gem->num_buckets;
  2541. assert(i < ARRAY_SIZE(bufmgr_gem->cache_bucket));
  2542. DRMINITLISTHEAD(&bufmgr_gem->cache_bucket[i].head);
  2543. bufmgr_gem->cache_bucket[i].size = size;
  2544. bufmgr_gem->num_buckets++;
  2545. }
  2546. static void
  2547. init_cache_buckets(drm_intel_bufmgr_gem *bufmgr_gem)
  2548. {
  2549. unsigned long size, cache_max_size = 64 * 1024 * 1024;
  2550. /* OK, so power of two buckets was too wasteful of memory.
  2551. * Give 3 other sizes between each power of two, to hopefully
  2552. * cover things accurately enough. (The alternative is
  2553. * probably to just go for exact matching of sizes, and assume
  2554. * that for things like composited window resize the tiled
  2555. * width/height alignment and rounding of sizes to pages will
  2556. * get us useful cache hit rates anyway)
  2557. */
  2558. add_bucket(bufmgr_gem, 4096);
  2559. add_bucket(bufmgr_gem, 4096 * 2);
  2560. add_bucket(bufmgr_gem, 4096 * 3);
  2561. /* Initialize the linked lists for BO reuse cache. */
  2562. for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
  2563. add_bucket(bufmgr_gem, size);
  2564. add_bucket(bufmgr_gem, size + size * 1 / 4);
  2565. add_bucket(bufmgr_gem, size + size * 2 / 4);
  2566. add_bucket(bufmgr_gem, size + size * 3 / 4);
  2567. }
  2568. }
  2569. drm_public void
  2570. drm_intel_bufmgr_gem_set_vma_cache_size(drm_intel_bufmgr *bufmgr, int limit)
  2571. {
  2572. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
  2573. bufmgr_gem->vma_max = limit;
  2574. drm_intel_gem_bo_purge_vma_cache(bufmgr_gem);
  2575. }
  2576. static int
  2577. parse_devid_override(const char *devid_override)
  2578. {
  2579. static const struct {
  2580. const char *name;
  2581. int pci_id;
  2582. } name_map[] = {
  2583. { "brw", PCI_CHIP_I965_GM },
  2584. { "g4x", PCI_CHIP_GM45_GM },
  2585. { "ilk", PCI_CHIP_ILD_G },
  2586. { "snb", PCI_CHIP_SANDYBRIDGE_M_GT2_PLUS },
  2587. { "ivb", PCI_CHIP_IVYBRIDGE_S_GT2 },
  2588. { "hsw", PCI_CHIP_HASWELL_CRW_E_GT3 },
  2589. { "byt", PCI_CHIP_VALLEYVIEW_3 },
  2590. { "bdw", 0x1620 | BDW_ULX },
  2591. { "skl", PCI_CHIP_SKYLAKE_DT_GT2 },
  2592. { "kbl", PCI_CHIP_KABYLAKE_DT_GT2 },
  2593. };
  2594. unsigned int i;
  2595. for (i = 0; i < ARRAY_SIZE(name_map); i++) {
  2596. if (!strcmp(name_map[i].name, devid_override))
  2597. return name_map[i].pci_id;
  2598. }
  2599. return strtod(devid_override, NULL);
  2600. }
  2601. /**
  2602. * Get the PCI ID for the device. This can be overridden by setting the
  2603. * INTEL_DEVID_OVERRIDE environment variable to the desired ID.
  2604. */
  2605. static int
  2606. get_pci_device_id(drm_intel_bufmgr_gem *bufmgr_gem)
  2607. {
  2608. char *devid_override;
  2609. int devid = 0;
  2610. int ret;
  2611. drm_i915_getparam_t gp;
  2612. if (geteuid() == getuid()) {
  2613. devid_override = getenv("INTEL_DEVID_OVERRIDE");
  2614. if (devid_override) {
  2615. bufmgr_gem->no_exec = true;
  2616. return parse_devid_override(devid_override);
  2617. }
  2618. }
  2619. memclear(gp);
  2620. gp.param = I915_PARAM_CHIPSET_ID;
  2621. gp.value = &devid;
  2622. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  2623. if (ret) {
  2624. fprintf(stderr, "get chip id failed: %d [%d]\n", ret, errno);
  2625. fprintf(stderr, "param: %d, val: %d\n", gp.param, *gp.value);
  2626. }
  2627. return devid;
  2628. }
  2629. drm_public int
  2630. drm_intel_bufmgr_gem_get_devid(drm_intel_bufmgr *bufmgr)
  2631. {
  2632. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
  2633. return bufmgr_gem->pci_device;
  2634. }
  2635. /**
  2636. * Sets the AUB filename.
  2637. *
  2638. * This function has to be called before drm_intel_bufmgr_gem_set_aub_dump()
  2639. * for it to have any effect.
  2640. */
  2641. drm_public void
  2642. drm_intel_bufmgr_gem_set_aub_filename(drm_intel_bufmgr *bufmgr,
  2643. const char *filename)
  2644. {
  2645. }
  2646. /**
  2647. * Sets up AUB dumping.
  2648. *
  2649. * This is a trace file format that can be used with the simulator.
  2650. * Packets are emitted in a format somewhat like GPU command packets.
  2651. * You can set up a GTT and upload your objects into the referenced
  2652. * space, then send off batchbuffers and get BMPs out the other end.
  2653. */
  2654. drm_public void
  2655. drm_intel_bufmgr_gem_set_aub_dump(drm_intel_bufmgr *bufmgr, int enable)
  2656. {
  2657. fprintf(stderr, "libdrm aub dumping is deprecated.\n\n"
  2658. "Use intel_aubdump from intel-gpu-tools instead. Install intel-gpu-tools,\n"
  2659. "then run (for example)\n\n"
  2660. "\t$ intel_aubdump --output=trace.aub glxgears -geometry 500x500\n\n"
  2661. "See the intel_aubdump man page for more details.\n");
  2662. }
  2663. drm_public drm_intel_context *
  2664. drm_intel_gem_context_create(drm_intel_bufmgr *bufmgr)
  2665. {
  2666. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
  2667. struct drm_i915_gem_context_create create;
  2668. drm_intel_context *context = NULL;
  2669. int ret;
  2670. context = calloc(1, sizeof(*context));
  2671. if (!context)
  2672. return NULL;
  2673. memclear(create);
  2674. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create);
  2675. if (ret != 0) {
  2676. DBG("DRM_IOCTL_I915_GEM_CONTEXT_CREATE failed: %s\n",
  2677. strerror(errno));
  2678. free(context);
  2679. return NULL;
  2680. }
  2681. context->ctx_id = create.ctx_id;
  2682. context->bufmgr = bufmgr;
  2683. return context;
  2684. }
  2685. drm_public int
  2686. drm_intel_gem_context_get_id(drm_intel_context *ctx, uint32_t *ctx_id)
  2687. {
  2688. if (ctx == NULL)
  2689. return -EINVAL;
  2690. *ctx_id = ctx->ctx_id;
  2691. return 0;
  2692. }
  2693. drm_public void
  2694. drm_intel_gem_context_destroy(drm_intel_context *ctx)
  2695. {
  2696. drm_intel_bufmgr_gem *bufmgr_gem;
  2697. struct drm_i915_gem_context_destroy destroy;
  2698. int ret;
  2699. if (ctx == NULL)
  2700. return;
  2701. memclear(destroy);
  2702. bufmgr_gem = (drm_intel_bufmgr_gem *)ctx->bufmgr;
  2703. destroy.ctx_id = ctx->ctx_id;
  2704. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_CONTEXT_DESTROY,
  2705. &destroy);
  2706. if (ret != 0)
  2707. fprintf(stderr, "DRM_IOCTL_I915_GEM_CONTEXT_DESTROY failed: %s\n",
  2708. strerror(errno));
  2709. free(ctx);
  2710. }
  2711. drm_public int
  2712. drm_intel_get_reset_stats(drm_intel_context *ctx,
  2713. uint32_t *reset_count,
  2714. uint32_t *active,
  2715. uint32_t *pending)
  2716. {
  2717. drm_intel_bufmgr_gem *bufmgr_gem;
  2718. struct drm_i915_reset_stats stats;
  2719. int ret;
  2720. if (ctx == NULL)
  2721. return -EINVAL;
  2722. memclear(stats);
  2723. bufmgr_gem = (drm_intel_bufmgr_gem *)ctx->bufmgr;
  2724. stats.ctx_id = ctx->ctx_id;
  2725. ret = drmIoctl(bufmgr_gem->fd,
  2726. DRM_IOCTL_I915_GET_RESET_STATS,
  2727. &stats);
  2728. if (ret == 0) {
  2729. if (reset_count != NULL)
  2730. *reset_count = stats.reset_count;
  2731. if (active != NULL)
  2732. *active = stats.batch_active;
  2733. if (pending != NULL)
  2734. *pending = stats.batch_pending;
  2735. }
  2736. return ret;
  2737. }
  2738. drm_public int
  2739. drm_intel_reg_read(drm_intel_bufmgr *bufmgr,
  2740. uint32_t offset,
  2741. uint64_t *result)
  2742. {
  2743. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
  2744. struct drm_i915_reg_read reg_read;
  2745. int ret;
  2746. memclear(reg_read);
  2747. reg_read.offset = offset;
  2748. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_REG_READ, &reg_read);
  2749. *result = reg_read.val;
  2750. return ret;
  2751. }
  2752. drm_public int
  2753. drm_intel_get_subslice_total(int fd, unsigned int *subslice_total)
  2754. {
  2755. drm_i915_getparam_t gp;
  2756. int ret;
  2757. memclear(gp);
  2758. gp.value = (int*)subslice_total;
  2759. gp.param = I915_PARAM_SUBSLICE_TOTAL;
  2760. ret = drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
  2761. if (ret)
  2762. return -errno;
  2763. return 0;
  2764. }
  2765. drm_public int
  2766. drm_intel_get_eu_total(int fd, unsigned int *eu_total)
  2767. {
  2768. drm_i915_getparam_t gp;
  2769. int ret;
  2770. memclear(gp);
  2771. gp.value = (int*)eu_total;
  2772. gp.param = I915_PARAM_EU_TOTAL;
  2773. ret = drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
  2774. if (ret)
  2775. return -errno;
  2776. return 0;
  2777. }
  2778. drm_public int
  2779. drm_intel_get_pooled_eu(int fd)
  2780. {
  2781. drm_i915_getparam_t gp;
  2782. int ret = -1;
  2783. memclear(gp);
  2784. gp.param = I915_PARAM_HAS_POOLED_EU;
  2785. gp.value = &ret;
  2786. if (drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp))
  2787. return -errno;
  2788. return ret;
  2789. }
  2790. drm_public int
  2791. drm_intel_get_min_eu_in_pool(int fd)
  2792. {
  2793. drm_i915_getparam_t gp;
  2794. int ret = -1;
  2795. memclear(gp);
  2796. gp.param = I915_PARAM_MIN_EU_IN_POOL;
  2797. gp.value = &ret;
  2798. if (drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp))
  2799. return -errno;
  2800. return ret;
  2801. }
  2802. /**
  2803. * Annotate the given bo for use in aub dumping.
  2804. *
  2805. * \param annotations is an array of drm_intel_aub_annotation objects
  2806. * describing the type of data in various sections of the bo. Each
  2807. * element of the array specifies the type and subtype of a section of
  2808. * the bo, and the past-the-end offset of that section. The elements
  2809. * of \c annotations must be sorted so that ending_offset is
  2810. * increasing.
  2811. *
  2812. * \param count is the number of elements in the \c annotations array.
  2813. * If \c count is zero, then \c annotations will not be dereferenced.
  2814. *
  2815. * Annotations are copied into a private data structure, so caller may
  2816. * re-use the memory pointed to by \c annotations after the call
  2817. * returns.
  2818. *
  2819. * Annotations are stored for the lifetime of the bo; to reset to the
  2820. * default state (no annotations), call this function with a \c count
  2821. * of zero.
  2822. */
  2823. drm_public void drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo,
  2824. drm_intel_aub_annotation *annotations,
  2825. unsigned count)
  2826. {
  2827. }
  2828. static pthread_mutex_t bufmgr_list_mutex = PTHREAD_MUTEX_INITIALIZER;
  2829. static drmMMListHead bufmgr_list = { &bufmgr_list, &bufmgr_list };
  2830. static drm_intel_bufmgr_gem *
  2831. drm_intel_bufmgr_gem_find(int fd)
  2832. {
  2833. drm_intel_bufmgr_gem *bufmgr_gem;
  2834. DRMLISTFOREACHENTRY(bufmgr_gem, &bufmgr_list, managers) {
  2835. if (bufmgr_gem->fd == fd) {
  2836. atomic_inc(&bufmgr_gem->refcount);
  2837. return bufmgr_gem;
  2838. }
  2839. }
  2840. return NULL;
  2841. }
  2842. static void
  2843. drm_intel_bufmgr_gem_unref(drm_intel_bufmgr *bufmgr)
  2844. {
  2845. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *)bufmgr;
  2846. if (atomic_add_unless(&bufmgr_gem->refcount, -1, 1))
  2847. return;
  2848. pthread_mutex_lock(&bufmgr_list_mutex);
  2849. if (atomic_dec_and_test(&bufmgr_gem->refcount)) {
  2850. DRMLISTDEL(&bufmgr_gem->managers);
  2851. drm_intel_bufmgr_gem_destroy(bufmgr);
  2852. }
  2853. pthread_mutex_unlock(&bufmgr_list_mutex);
  2854. }
  2855. drm_public void *drm_intel_gem_bo_map__gtt(drm_intel_bo *bo)
  2856. {
  2857. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  2858. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2859. if (bo_gem->gtt_virtual)
  2860. return bo_gem->gtt_virtual;
  2861. if (bo_gem->is_userptr)
  2862. return NULL;
  2863. pthread_mutex_lock(&bufmgr_gem->lock);
  2864. if (bo_gem->gtt_virtual == NULL) {
  2865. struct drm_i915_gem_mmap_gtt mmap_arg;
  2866. void *ptr;
  2867. DBG("bo_map_gtt: mmap %d (%s), map_count=%d\n",
  2868. bo_gem->gem_handle, bo_gem->name, bo_gem->map_count);
  2869. if (bo_gem->map_count++ == 0)
  2870. drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
  2871. memclear(mmap_arg);
  2872. mmap_arg.handle = bo_gem->gem_handle;
  2873. /* Get the fake offset back... */
  2874. ptr = MAP_FAILED;
  2875. if (drmIoctl(bufmgr_gem->fd,
  2876. DRM_IOCTL_I915_GEM_MMAP_GTT,
  2877. &mmap_arg) == 0) {
  2878. /* and mmap it */
  2879. ptr = drm_mmap(0, bo->size, PROT_READ | PROT_WRITE,
  2880. MAP_SHARED, bufmgr_gem->fd,
  2881. mmap_arg.offset);
  2882. }
  2883. if (ptr == MAP_FAILED) {
  2884. if (--bo_gem->map_count == 0)
  2885. drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
  2886. ptr = NULL;
  2887. }
  2888. bo_gem->gtt_virtual = ptr;
  2889. }
  2890. pthread_mutex_unlock(&bufmgr_gem->lock);
  2891. return bo_gem->gtt_virtual;
  2892. }
  2893. drm_public void *drm_intel_gem_bo_map__cpu(drm_intel_bo *bo)
  2894. {
  2895. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  2896. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2897. if (bo_gem->mem_virtual)
  2898. return bo_gem->mem_virtual;
  2899. if (bo_gem->is_userptr) {
  2900. /* Return the same user ptr */
  2901. return bo_gem->user_virtual;
  2902. }
  2903. pthread_mutex_lock(&bufmgr_gem->lock);
  2904. if (!bo_gem->mem_virtual) {
  2905. struct drm_i915_gem_mmap mmap_arg;
  2906. if (bo_gem->map_count++ == 0)
  2907. drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
  2908. DBG("bo_map: %d (%s), map_count=%d\n",
  2909. bo_gem->gem_handle, bo_gem->name, bo_gem->map_count);
  2910. memclear(mmap_arg);
  2911. mmap_arg.handle = bo_gem->gem_handle;
  2912. mmap_arg.size = bo->size;
  2913. if (drmIoctl(bufmgr_gem->fd,
  2914. DRM_IOCTL_I915_GEM_MMAP,
  2915. &mmap_arg)) {
  2916. DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
  2917. __FILE__, __LINE__, bo_gem->gem_handle,
  2918. bo_gem->name, strerror(errno));
  2919. if (--bo_gem->map_count == 0)
  2920. drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
  2921. } else {
  2922. VG(VALGRIND_MALLOCLIKE_BLOCK(mmap_arg.addr_ptr, mmap_arg.size, 0, 1));
  2923. bo_gem->mem_virtual = (void *)(uintptr_t) mmap_arg.addr_ptr;
  2924. }
  2925. }
  2926. pthread_mutex_unlock(&bufmgr_gem->lock);
  2927. return bo_gem->mem_virtual;
  2928. }
  2929. drm_public void *drm_intel_gem_bo_map__wc(drm_intel_bo *bo)
  2930. {
  2931. drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
  2932. drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  2933. if (bo_gem->wc_virtual)
  2934. return bo_gem->wc_virtual;
  2935. if (bo_gem->is_userptr)
  2936. return NULL;
  2937. pthread_mutex_lock(&bufmgr_gem->lock);
  2938. if (!bo_gem->wc_virtual) {
  2939. struct drm_i915_gem_mmap mmap_arg;
  2940. if (bo_gem->map_count++ == 0)
  2941. drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
  2942. DBG("bo_map: %d (%s), map_count=%d\n",
  2943. bo_gem->gem_handle, bo_gem->name, bo_gem->map_count);
  2944. memclear(mmap_arg);
  2945. mmap_arg.handle = bo_gem->gem_handle;
  2946. mmap_arg.size = bo->size;
  2947. mmap_arg.flags = I915_MMAP_WC;
  2948. if (drmIoctl(bufmgr_gem->fd,
  2949. DRM_IOCTL_I915_GEM_MMAP,
  2950. &mmap_arg)) {
  2951. DBG("%s:%d: Error mapping buffer %d (%s): %s .\n",
  2952. __FILE__, __LINE__, bo_gem->gem_handle,
  2953. bo_gem->name, strerror(errno));
  2954. if (--bo_gem->map_count == 0)
  2955. drm_intel_gem_bo_close_vma(bufmgr_gem, bo_gem);
  2956. } else {
  2957. VG(VALGRIND_MALLOCLIKE_BLOCK(mmap_arg.addr_ptr, mmap_arg.size, 0, 1));
  2958. bo_gem->wc_virtual = (void *)(uintptr_t) mmap_arg.addr_ptr;
  2959. }
  2960. }
  2961. pthread_mutex_unlock(&bufmgr_gem->lock);
  2962. return bo_gem->wc_virtual;
  2963. }
  2964. /**
  2965. * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
  2966. * and manage map buffer objections.
  2967. *
  2968. * \param fd File descriptor of the opened DRM device.
  2969. */
  2970. drm_public drm_intel_bufmgr *
  2971. drm_intel_bufmgr_gem_init(int fd, int batch_size)
  2972. {
  2973. drm_intel_bufmgr_gem *bufmgr_gem;
  2974. struct drm_i915_gem_get_aperture aperture;
  2975. drm_i915_getparam_t gp;
  2976. int ret, tmp;
  2977. pthread_mutex_lock(&bufmgr_list_mutex);
  2978. bufmgr_gem = drm_intel_bufmgr_gem_find(fd);
  2979. if (bufmgr_gem)
  2980. goto exit;
  2981. bufmgr_gem = calloc(1, sizeof(*bufmgr_gem));
  2982. if (bufmgr_gem == NULL)
  2983. goto exit;
  2984. bufmgr_gem->fd = fd;
  2985. atomic_set(&bufmgr_gem->refcount, 1);
  2986. if (pthread_mutex_init(&bufmgr_gem->lock, NULL) != 0) {
  2987. free(bufmgr_gem);
  2988. bufmgr_gem = NULL;
  2989. goto exit;
  2990. }
  2991. memclear(aperture);
  2992. ret = drmIoctl(bufmgr_gem->fd,
  2993. DRM_IOCTL_I915_GEM_GET_APERTURE,
  2994. &aperture);
  2995. if (ret == 0)
  2996. bufmgr_gem->gtt_size = aperture.aper_available_size;
  2997. else {
  2998. fprintf(stderr, "DRM_IOCTL_I915_GEM_APERTURE failed: %s\n",
  2999. strerror(errno));
  3000. bufmgr_gem->gtt_size = 128 * 1024 * 1024;
  3001. fprintf(stderr, "Assuming %dkB available aperture size.\n"
  3002. "May lead to reduced performance or incorrect "
  3003. "rendering.\n",
  3004. (int)bufmgr_gem->gtt_size / 1024);
  3005. }
  3006. bufmgr_gem->pci_device = get_pci_device_id(bufmgr_gem);
  3007. if (IS_GEN2(bufmgr_gem->pci_device))
  3008. bufmgr_gem->gen = 2;
  3009. else if (IS_GEN3(bufmgr_gem->pci_device))
  3010. bufmgr_gem->gen = 3;
  3011. else if (IS_GEN4(bufmgr_gem->pci_device))
  3012. bufmgr_gem->gen = 4;
  3013. else if (IS_GEN5(bufmgr_gem->pci_device))
  3014. bufmgr_gem->gen = 5;
  3015. else if (IS_GEN6(bufmgr_gem->pci_device))
  3016. bufmgr_gem->gen = 6;
  3017. else if (IS_GEN7(bufmgr_gem->pci_device))
  3018. bufmgr_gem->gen = 7;
  3019. else
  3020. /* Treat all further unmatched platforms the same as gen8 */
  3021. bufmgr_gem->gen = 8;
  3022. if (IS_GEN3(bufmgr_gem->pci_device) &&
  3023. bufmgr_gem->gtt_size > 256*1024*1024) {
  3024. /* The unmappable part of gtt on gen 3 (i.e. above 256MB) can't
  3025. * be used for tiled blits. To simplify the accounting, just
  3026. * subtract the unmappable part (fixed to 256MB on all known
  3027. * gen3 devices) if the kernel advertises it. */
  3028. bufmgr_gem->gtt_size -= 256*1024*1024;
  3029. }
  3030. memclear(gp);
  3031. gp.value = &tmp;
  3032. gp.param = I915_PARAM_HAS_EXECBUF2;
  3033. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  3034. if (ret) {
  3035. fprintf(stderr, "i915 does not support EXECBUFER2\n");
  3036. free(bufmgr_gem);
  3037. bufmgr_gem = NULL;
  3038. goto exit;
  3039. }
  3040. gp.param = I915_PARAM_HAS_BSD;
  3041. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  3042. bufmgr_gem->has_bsd = ret == 0;
  3043. gp.param = I915_PARAM_HAS_BLT;
  3044. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  3045. bufmgr_gem->has_blt = ret == 0;
  3046. gp.param = I915_PARAM_HAS_RELAXED_FENCING;
  3047. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  3048. bufmgr_gem->has_relaxed_fencing = ret == 0;
  3049. gp.param = I915_PARAM_HAS_EXEC_ASYNC;
  3050. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  3051. bufmgr_gem->has_exec_async = ret == 0;
  3052. bufmgr_gem->bufmgr.bo_alloc_userptr = check_bo_alloc_userptr;
  3053. gp.param = I915_PARAM_HAS_WAIT_TIMEOUT;
  3054. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  3055. bufmgr_gem->has_wait_timeout = ret == 0;
  3056. gp.param = I915_PARAM_HAS_LLC;
  3057. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  3058. if (ret != 0) {
  3059. /* Kernel does not supports HAS_LLC query, fallback to GPU
  3060. * generation detection and assume that we have LLC on GEN6/7
  3061. */
  3062. bufmgr_gem->has_llc = (IS_GEN6(bufmgr_gem->pci_device) |
  3063. IS_GEN7(bufmgr_gem->pci_device));
  3064. } else
  3065. bufmgr_gem->has_llc = *gp.value;
  3066. gp.param = I915_PARAM_HAS_VEBOX;
  3067. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  3068. bufmgr_gem->has_vebox = (ret == 0) & (*gp.value > 0);
  3069. gp.param = I915_PARAM_HAS_EXEC_SOFTPIN;
  3070. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  3071. if (ret == 0 && *gp.value > 0)
  3072. bufmgr_gem->bufmgr.bo_set_softpin_offset = drm_intel_gem_bo_set_softpin_offset;
  3073. if (bufmgr_gem->gen < 4) {
  3074. gp.param = I915_PARAM_NUM_FENCES_AVAIL;
  3075. gp.value = &bufmgr_gem->available_fences;
  3076. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  3077. if (ret) {
  3078. fprintf(stderr, "get fences failed: %d [%d]\n", ret,
  3079. errno);
  3080. fprintf(stderr, "param: %d, val: %d\n", gp.param,
  3081. *gp.value);
  3082. bufmgr_gem->available_fences = 0;
  3083. } else {
  3084. /* XXX The kernel reports the total number of fences,
  3085. * including any that may be pinned.
  3086. *
  3087. * We presume that there will be at least one pinned
  3088. * fence for the scanout buffer, but there may be more
  3089. * than one scanout and the user may be manually
  3090. * pinning buffers. Let's move to execbuffer2 and
  3091. * thereby forget the insanity of using fences...
  3092. */
  3093. bufmgr_gem->available_fences -= 2;
  3094. if (bufmgr_gem->available_fences < 0)
  3095. bufmgr_gem->available_fences = 0;
  3096. }
  3097. }
  3098. if (bufmgr_gem->gen >= 8) {
  3099. gp.param = I915_PARAM_HAS_ALIASING_PPGTT;
  3100. ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  3101. if (ret == 0 && *gp.value == 3)
  3102. bufmgr_gem->bufmgr.bo_use_48b_address_range = drm_intel_gem_bo_use_48b_address_range;
  3103. }
  3104. /* Let's go with one relocation per every 2 dwords (but round down a bit
  3105. * since a power of two will mean an extra page allocation for the reloc
  3106. * buffer).
  3107. *
  3108. * Every 4 was too few for the blender benchmark.
  3109. */
  3110. bufmgr_gem->max_relocs = batch_size / sizeof(uint32_t) / 2 - 2;
  3111. bufmgr_gem->bufmgr.bo_alloc = drm_intel_gem_bo_alloc;
  3112. bufmgr_gem->bufmgr.bo_alloc_for_render =
  3113. drm_intel_gem_bo_alloc_for_render;
  3114. bufmgr_gem->bufmgr.bo_alloc_tiled = drm_intel_gem_bo_alloc_tiled;
  3115. bufmgr_gem->bufmgr.bo_reference = drm_intel_gem_bo_reference;
  3116. bufmgr_gem->bufmgr.bo_unreference = drm_intel_gem_bo_unreference;
  3117. bufmgr_gem->bufmgr.bo_map = drm_intel_gem_bo_map;
  3118. bufmgr_gem->bufmgr.bo_unmap = drm_intel_gem_bo_unmap;
  3119. bufmgr_gem->bufmgr.bo_subdata = drm_intel_gem_bo_subdata;
  3120. bufmgr_gem->bufmgr.bo_get_subdata = drm_intel_gem_bo_get_subdata;
  3121. bufmgr_gem->bufmgr.bo_wait_rendering = drm_intel_gem_bo_wait_rendering;
  3122. bufmgr_gem->bufmgr.bo_emit_reloc = drm_intel_gem_bo_emit_reloc;
  3123. bufmgr_gem->bufmgr.bo_emit_reloc_fence = drm_intel_gem_bo_emit_reloc_fence;
  3124. bufmgr_gem->bufmgr.bo_pin = drm_intel_gem_bo_pin;
  3125. bufmgr_gem->bufmgr.bo_unpin = drm_intel_gem_bo_unpin;
  3126. bufmgr_gem->bufmgr.bo_get_tiling = drm_intel_gem_bo_get_tiling;
  3127. bufmgr_gem->bufmgr.bo_set_tiling = drm_intel_gem_bo_set_tiling;
  3128. bufmgr_gem->bufmgr.bo_flink = drm_intel_gem_bo_flink;
  3129. bufmgr_gem->bufmgr.bo_exec = drm_intel_gem_bo_exec2;
  3130. bufmgr_gem->bufmgr.bo_mrb_exec = drm_intel_gem_bo_mrb_exec2;
  3131. bufmgr_gem->bufmgr.bo_busy = drm_intel_gem_bo_busy;
  3132. bufmgr_gem->bufmgr.bo_madvise = drm_intel_gem_bo_madvise;
  3133. bufmgr_gem->bufmgr.destroy = drm_intel_bufmgr_gem_unref;
  3134. bufmgr_gem->bufmgr.debug = 0;
  3135. bufmgr_gem->bufmgr.check_aperture_space =
  3136. drm_intel_gem_check_aperture_space;
  3137. bufmgr_gem->bufmgr.bo_disable_reuse = drm_intel_gem_bo_disable_reuse;
  3138. bufmgr_gem->bufmgr.bo_is_reusable = drm_intel_gem_bo_is_reusable;
  3139. bufmgr_gem->bufmgr.get_pipe_from_crtc_id =
  3140. drm_intel_gem_get_pipe_from_crtc_id;
  3141. bufmgr_gem->bufmgr.bo_references = drm_intel_gem_bo_references;
  3142. init_cache_buckets(bufmgr_gem);
  3143. DRMINITLISTHEAD(&bufmgr_gem->vma_cache);
  3144. bufmgr_gem->vma_max = -1; /* unlimited by default */
  3145. DRMLISTADD(&bufmgr_gem->managers, &bufmgr_list);
  3146. exit:
  3147. pthread_mutex_unlock(&bufmgr_list_mutex);
  3148. return bufmgr_gem != NULL ? &bufmgr_gem->bufmgr : NULL;
  3149. }