Clone of mesa.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

anv_device.c 117KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306
  1. /*
  2. * Copyright © 2015 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. */
  23. #include <assert.h>
  24. #include <stdbool.h>
  25. #include <string.h>
  26. #include <sys/mman.h>
  27. #include <sys/sysinfo.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <xf86drm.h>
  31. #include <drm_fourcc.h>
  32. #include "anv_private.h"
  33. #include "util/strtod.h"
  34. #include "util/debug.h"
  35. #include "util/build_id.h"
  36. #include "util/disk_cache.h"
  37. #include "util/mesa-sha1.h"
  38. #include "util/u_string.h"
  39. #include "git_sha1.h"
  40. #include "vk_util.h"
  41. #include "common/gen_defines.h"
  42. #include "genxml/gen7_pack.h"
  43. static void
  44. compiler_debug_log(void *data, const char *fmt, ...)
  45. { }
  46. static void
  47. compiler_perf_log(void *data, const char *fmt, ...)
  48. {
  49. va_list args;
  50. va_start(args, fmt);
  51. if (unlikely(INTEL_DEBUG & DEBUG_PERF))
  52. intel_logd_v(fmt, args);
  53. va_end(args);
  54. }
  55. static VkResult
  56. anv_compute_heap_size(int fd, uint64_t gtt_size, uint64_t *heap_size)
  57. {
  58. /* Query the total ram from the system */
  59. struct sysinfo info;
  60. sysinfo(&info);
  61. uint64_t total_ram = (uint64_t)info.totalram * (uint64_t)info.mem_unit;
  62. /* We don't want to burn too much ram with the GPU. If the user has 4GiB
  63. * or less, we use at most half. If they have more than 4GiB, we use 3/4.
  64. */
  65. uint64_t available_ram;
  66. if (total_ram <= 4ull * 1024ull * 1024ull * 1024ull)
  67. available_ram = total_ram / 2;
  68. else
  69. available_ram = total_ram * 3 / 4;
  70. /* We also want to leave some padding for things we allocate in the driver,
  71. * so don't go over 3/4 of the GTT either.
  72. */
  73. uint64_t available_gtt = gtt_size * 3 / 4;
  74. *heap_size = MIN2(available_ram, available_gtt);
  75. return VK_SUCCESS;
  76. }
  77. static VkResult
  78. anv_physical_device_init_heaps(struct anv_physical_device *device, int fd)
  79. {
  80. uint64_t gtt_size;
  81. if (anv_gem_get_context_param(fd, 0, I915_CONTEXT_PARAM_GTT_SIZE,
  82. &gtt_size) == -1) {
  83. /* If, for whatever reason, we can't actually get the GTT size from the
  84. * kernel (too old?) fall back to the aperture size.
  85. */
  86. anv_perf_warn(NULL, NULL,
  87. "Failed to get I915_CONTEXT_PARAM_GTT_SIZE: %m");
  88. if (anv_gem_get_aperture(fd, &gtt_size) == -1) {
  89. return vk_errorf(NULL, NULL, VK_ERROR_INITIALIZATION_FAILED,
  90. "failed to get aperture size: %m");
  91. }
  92. }
  93. device->supports_48bit_addresses = (device->info.gen >= 8) &&
  94. gtt_size > (4ULL << 30 /* GiB */);
  95. uint64_t heap_size = 0;
  96. VkResult result = anv_compute_heap_size(fd, gtt_size, &heap_size);
  97. if (result != VK_SUCCESS)
  98. return result;
  99. if (heap_size > (2ull << 30) && !device->supports_48bit_addresses) {
  100. /* When running with an overridden PCI ID, we may get a GTT size from
  101. * the kernel that is greater than 2 GiB but the execbuf check for 48bit
  102. * address support can still fail. Just clamp the address space size to
  103. * 2 GiB if we don't have 48-bit support.
  104. */
  105. intel_logw("%s:%d: The kernel reported a GTT size larger than 2 GiB but "
  106. "not support for 48-bit addresses",
  107. __FILE__, __LINE__);
  108. heap_size = 2ull << 30;
  109. }
  110. if (heap_size <= 3ull * (1ull << 30)) {
  111. /* In this case, everything fits nicely into the 32-bit address space,
  112. * so there's no need for supporting 48bit addresses on client-allocated
  113. * memory objects.
  114. */
  115. device->memory.heap_count = 1;
  116. device->memory.heaps[0] = (struct anv_memory_heap) {
  117. .size = heap_size,
  118. .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
  119. .supports_48bit_addresses = false,
  120. };
  121. } else {
  122. /* Not everything will fit nicely into a 32-bit address space. In this
  123. * case we need a 64-bit heap. Advertise a small 32-bit heap and a
  124. * larger 48-bit heap. If we're in this case, then we have a total heap
  125. * size larger than 3GiB which most likely means they have 8 GiB of
  126. * video memory and so carving off 1 GiB for the 32-bit heap should be
  127. * reasonable.
  128. */
  129. const uint64_t heap_size_32bit = 1ull << 30;
  130. const uint64_t heap_size_48bit = heap_size - heap_size_32bit;
  131. assert(device->supports_48bit_addresses);
  132. device->memory.heap_count = 2;
  133. device->memory.heaps[0] = (struct anv_memory_heap) {
  134. .size = heap_size_48bit,
  135. .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
  136. .supports_48bit_addresses = true,
  137. };
  138. device->memory.heaps[1] = (struct anv_memory_heap) {
  139. .size = heap_size_32bit,
  140. .flags = VK_MEMORY_HEAP_DEVICE_LOCAL_BIT,
  141. .supports_48bit_addresses = false,
  142. };
  143. }
  144. uint32_t type_count = 0;
  145. for (uint32_t heap = 0; heap < device->memory.heap_count; heap++) {
  146. uint32_t valid_buffer_usage = ~0;
  147. /* There appears to be a hardware issue in the VF cache where it only
  148. * considers the bottom 32 bits of memory addresses. If you happen to
  149. * have two vertex buffers which get placed exactly 4 GiB apart and use
  150. * them in back-to-back draw calls, you can get collisions. In order to
  151. * solve this problem, we require vertex and index buffers be bound to
  152. * memory allocated out of the 32-bit heap.
  153. */
  154. if (device->memory.heaps[heap].supports_48bit_addresses) {
  155. valid_buffer_usage &= ~(VK_BUFFER_USAGE_INDEX_BUFFER_BIT |
  156. VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
  157. }
  158. if (device->info.has_llc) {
  159. /* Big core GPUs share LLC with the CPU and thus one memory type can be
  160. * both cached and coherent at the same time.
  161. */
  162. device->memory.types[type_count++] = (struct anv_memory_type) {
  163. .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
  164. VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
  165. VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
  166. VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
  167. .heapIndex = heap,
  168. .valid_buffer_usage = valid_buffer_usage,
  169. };
  170. } else {
  171. /* The spec requires that we expose a host-visible, coherent memory
  172. * type, but Atom GPUs don't share LLC. Thus we offer two memory types
  173. * to give the application a choice between cached, but not coherent and
  174. * coherent but uncached (WC though).
  175. */
  176. device->memory.types[type_count++] = (struct anv_memory_type) {
  177. .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
  178. VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
  179. VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
  180. .heapIndex = heap,
  181. .valid_buffer_usage = valid_buffer_usage,
  182. };
  183. device->memory.types[type_count++] = (struct anv_memory_type) {
  184. .propertyFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT |
  185. VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
  186. VK_MEMORY_PROPERTY_HOST_CACHED_BIT,
  187. .heapIndex = heap,
  188. .valid_buffer_usage = valid_buffer_usage,
  189. };
  190. }
  191. }
  192. device->memory.type_count = type_count;
  193. return VK_SUCCESS;
  194. }
  195. static VkResult
  196. anv_physical_device_init_uuids(struct anv_physical_device *device)
  197. {
  198. const struct build_id_note *note =
  199. build_id_find_nhdr_for_addr(anv_physical_device_init_uuids);
  200. if (!note) {
  201. return vk_errorf(device->instance, device,
  202. VK_ERROR_INITIALIZATION_FAILED,
  203. "Failed to find build-id");
  204. }
  205. unsigned build_id_len = build_id_length(note);
  206. if (build_id_len < 20) {
  207. return vk_errorf(device->instance, device,
  208. VK_ERROR_INITIALIZATION_FAILED,
  209. "build-id too short. It needs to be a SHA");
  210. }
  211. memcpy(device->driver_build_sha1, build_id_data(note), 20);
  212. struct mesa_sha1 sha1_ctx;
  213. uint8_t sha1[20];
  214. STATIC_ASSERT(VK_UUID_SIZE <= sizeof(sha1));
  215. /* The pipeline cache UUID is used for determining when a pipeline cache is
  216. * invalid. It needs both a driver build and the PCI ID of the device.
  217. */
  218. _mesa_sha1_init(&sha1_ctx);
  219. _mesa_sha1_update(&sha1_ctx, build_id_data(note), build_id_len);
  220. _mesa_sha1_update(&sha1_ctx, &device->chipset_id,
  221. sizeof(device->chipset_id));
  222. _mesa_sha1_final(&sha1_ctx, sha1);
  223. memcpy(device->pipeline_cache_uuid, sha1, VK_UUID_SIZE);
  224. /* The driver UUID is used for determining sharability of images and memory
  225. * between two Vulkan instances in separate processes. People who want to
  226. * share memory need to also check the device UUID (below) so all this
  227. * needs to be is the build-id.
  228. */
  229. memcpy(device->driver_uuid, build_id_data(note), VK_UUID_SIZE);
  230. /* The device UUID uniquely identifies the given device within the machine.
  231. * Since we never have more than one device, this doesn't need to be a real
  232. * UUID. However, on the off-chance that someone tries to use this to
  233. * cache pre-tiled images or something of the like, we use the PCI ID and
  234. * some bits of ISL info to ensure that this is safe.
  235. */
  236. _mesa_sha1_init(&sha1_ctx);
  237. _mesa_sha1_update(&sha1_ctx, &device->chipset_id,
  238. sizeof(device->chipset_id));
  239. _mesa_sha1_update(&sha1_ctx, &device->isl_dev.has_bit6_swizzling,
  240. sizeof(device->isl_dev.has_bit6_swizzling));
  241. _mesa_sha1_final(&sha1_ctx, sha1);
  242. memcpy(device->device_uuid, sha1, VK_UUID_SIZE);
  243. return VK_SUCCESS;
  244. }
  245. static void
  246. anv_physical_device_init_disk_cache(struct anv_physical_device *device)
  247. {
  248. #ifdef ENABLE_SHADER_CACHE
  249. char renderer[10];
  250. MAYBE_UNUSED int len = snprintf(renderer, sizeof(renderer), "anv_%04x",
  251. device->chipset_id);
  252. assert(len == sizeof(renderer) - 2);
  253. char timestamp[41];
  254. _mesa_sha1_format(timestamp, device->driver_build_sha1);
  255. const uint64_t driver_flags =
  256. brw_get_compiler_config_value(device->compiler);
  257. device->disk_cache = disk_cache_create(renderer, timestamp, driver_flags);
  258. #else
  259. device->disk_cache = NULL;
  260. #endif
  261. }
  262. static void
  263. anv_physical_device_free_disk_cache(struct anv_physical_device *device)
  264. {
  265. #ifdef ENABLE_SHADER_CACHE
  266. if (device->disk_cache)
  267. disk_cache_destroy(device->disk_cache);
  268. #else
  269. assert(device->disk_cache == NULL);
  270. #endif
  271. }
  272. static VkResult
  273. anv_physical_device_init(struct anv_physical_device *device,
  274. struct anv_instance *instance,
  275. drmDevicePtr drm_device)
  276. {
  277. const char *primary_path = drm_device->nodes[DRM_NODE_PRIMARY];
  278. const char *path = drm_device->nodes[DRM_NODE_RENDER];
  279. VkResult result;
  280. int fd;
  281. int master_fd = -1;
  282. brw_process_intel_debug_variable();
  283. fd = open(path, O_RDWR | O_CLOEXEC);
  284. if (fd < 0)
  285. return vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
  286. device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
  287. device->instance = instance;
  288. assert(strlen(path) < ARRAY_SIZE(device->path));
  289. snprintf(device->path, ARRAY_SIZE(device->path), "%s", path);
  290. device->no_hw = getenv("INTEL_NO_HW") != NULL;
  291. const int pci_id_override = gen_get_pci_device_id_override();
  292. if (pci_id_override < 0) {
  293. device->chipset_id = anv_gem_get_param(fd, I915_PARAM_CHIPSET_ID);
  294. if (!device->chipset_id) {
  295. result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
  296. goto fail;
  297. }
  298. } else {
  299. device->chipset_id = pci_id_override;
  300. device->no_hw = true;
  301. }
  302. device->pci_info.domain = drm_device->businfo.pci->domain;
  303. device->pci_info.bus = drm_device->businfo.pci->bus;
  304. device->pci_info.device = drm_device->businfo.pci->dev;
  305. device->pci_info.function = drm_device->businfo.pci->func;
  306. device->name = gen_get_device_name(device->chipset_id);
  307. if (!gen_get_device_info(device->chipset_id, &device->info)) {
  308. result = vk_error(VK_ERROR_INCOMPATIBLE_DRIVER);
  309. goto fail;
  310. }
  311. if (device->info.is_haswell) {
  312. intel_logw("Haswell Vulkan support is incomplete");
  313. } else if (device->info.gen == 7 && !device->info.is_baytrail) {
  314. intel_logw("Ivy Bridge Vulkan support is incomplete");
  315. } else if (device->info.gen == 7 && device->info.is_baytrail) {
  316. intel_logw("Bay Trail Vulkan support is incomplete");
  317. } else if (device->info.gen >= 8 && device->info.gen <= 10) {
  318. /* Gen8-10 fully supported */
  319. } else if (device->info.gen == 11) {
  320. intel_logw("Vulkan is not yet fully supported on gen11.");
  321. } else {
  322. result = vk_errorf(device->instance, device,
  323. VK_ERROR_INCOMPATIBLE_DRIVER,
  324. "Vulkan not yet supported on %s", device->name);
  325. goto fail;
  326. }
  327. device->cmd_parser_version = -1;
  328. if (device->info.gen == 7) {
  329. device->cmd_parser_version =
  330. anv_gem_get_param(fd, I915_PARAM_CMD_PARSER_VERSION);
  331. if (device->cmd_parser_version == -1) {
  332. result = vk_errorf(device->instance, device,
  333. VK_ERROR_INITIALIZATION_FAILED,
  334. "failed to get command parser version");
  335. goto fail;
  336. }
  337. }
  338. if (!anv_gem_get_param(fd, I915_PARAM_HAS_WAIT_TIMEOUT)) {
  339. result = vk_errorf(device->instance, device,
  340. VK_ERROR_INITIALIZATION_FAILED,
  341. "kernel missing gem wait");
  342. goto fail;
  343. }
  344. if (!anv_gem_get_param(fd, I915_PARAM_HAS_EXECBUF2)) {
  345. result = vk_errorf(device->instance, device,
  346. VK_ERROR_INITIALIZATION_FAILED,
  347. "kernel missing execbuf2");
  348. goto fail;
  349. }
  350. if (!device->info.has_llc &&
  351. anv_gem_get_param(fd, I915_PARAM_MMAP_VERSION) < 1) {
  352. result = vk_errorf(device->instance, device,
  353. VK_ERROR_INITIALIZATION_FAILED,
  354. "kernel missing wc mmap");
  355. goto fail;
  356. }
  357. result = anv_physical_device_init_heaps(device, fd);
  358. if (result != VK_SUCCESS)
  359. goto fail;
  360. device->has_exec_async = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_ASYNC);
  361. device->has_exec_capture = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_CAPTURE);
  362. device->has_exec_fence = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_FENCE);
  363. device->has_syncobj = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_FENCE_ARRAY);
  364. device->has_syncobj_wait = device->has_syncobj &&
  365. anv_gem_supports_syncobj_wait(fd);
  366. device->has_context_priority = anv_gem_has_context_priority(fd);
  367. device->use_softpin = anv_gem_get_param(fd, I915_PARAM_HAS_EXEC_SOFTPIN)
  368. && device->supports_48bit_addresses;
  369. device->has_context_isolation =
  370. anv_gem_get_param(fd, I915_PARAM_HAS_CONTEXT_ISOLATION);
  371. bool swizzled = anv_gem_get_bit6_swizzle(fd, I915_TILING_X);
  372. /* Starting with Gen10, the timestamp frequency of the command streamer may
  373. * vary from one part to another. We can query the value from the kernel.
  374. */
  375. if (device->info.gen >= 10) {
  376. int timestamp_frequency =
  377. anv_gem_get_param(fd, I915_PARAM_CS_TIMESTAMP_FREQUENCY);
  378. if (timestamp_frequency < 0)
  379. intel_logw("Kernel 4.16-rc1+ required to properly query CS timestamp frequency");
  380. else
  381. device->info.timestamp_frequency = timestamp_frequency;
  382. }
  383. /* GENs prior to 8 do not support EU/Subslice info */
  384. if (device->info.gen >= 8) {
  385. device->subslice_total = anv_gem_get_param(fd, I915_PARAM_SUBSLICE_TOTAL);
  386. device->eu_total = anv_gem_get_param(fd, I915_PARAM_EU_TOTAL);
  387. /* Without this information, we cannot get the right Braswell
  388. * brandstrings, and we have to use conservative numbers for GPGPU on
  389. * many platforms, but otherwise, things will just work.
  390. */
  391. if (device->subslice_total < 1 || device->eu_total < 1) {
  392. intel_logw("Kernel 4.1 required to properly query GPU properties");
  393. }
  394. } else if (device->info.gen == 7) {
  395. device->subslice_total = 1 << (device->info.gt - 1);
  396. }
  397. if (device->info.is_cherryview &&
  398. device->subslice_total > 0 && device->eu_total > 0) {
  399. /* Logical CS threads = EUs per subslice * num threads per EU */
  400. uint32_t max_cs_threads =
  401. device->eu_total / device->subslice_total * device->info.num_thread_per_eu;
  402. /* Fuse configurations may give more threads than expected, never less. */
  403. if (max_cs_threads > device->info.max_cs_threads)
  404. device->info.max_cs_threads = max_cs_threads;
  405. }
  406. device->compiler = brw_compiler_create(NULL, &device->info);
  407. if (device->compiler == NULL) {
  408. result = vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
  409. goto fail;
  410. }
  411. device->compiler->shader_debug_log = compiler_debug_log;
  412. device->compiler->shader_perf_log = compiler_perf_log;
  413. device->compiler->supports_pull_constants = false;
  414. device->compiler->constant_buffer_0_is_relative =
  415. device->info.gen < 8 || !device->has_context_isolation;
  416. device->compiler->supports_shader_constants = true;
  417. isl_device_init(&device->isl_dev, &device->info, swizzled);
  418. result = anv_physical_device_init_uuids(device);
  419. if (result != VK_SUCCESS)
  420. goto fail;
  421. anv_physical_device_init_disk_cache(device);
  422. if (instance->enabled_extensions.KHR_display) {
  423. master_fd = open(primary_path, O_RDWR | O_CLOEXEC);
  424. if (master_fd >= 0) {
  425. /* prod the device with a GETPARAM call which will fail if
  426. * we don't have permission to even render on this device
  427. */
  428. if (anv_gem_get_param(master_fd, I915_PARAM_CHIPSET_ID) == 0) {
  429. close(master_fd);
  430. master_fd = -1;
  431. }
  432. }
  433. }
  434. device->master_fd = master_fd;
  435. result = anv_init_wsi(device);
  436. if (result != VK_SUCCESS) {
  437. ralloc_free(device->compiler);
  438. anv_physical_device_free_disk_cache(device);
  439. goto fail;
  440. }
  441. anv_physical_device_get_supported_extensions(device,
  442. &device->supported_extensions);
  443. device->local_fd = fd;
  444. return VK_SUCCESS;
  445. fail:
  446. close(fd);
  447. if (master_fd != -1)
  448. close(master_fd);
  449. return result;
  450. }
  451. static void
  452. anv_physical_device_finish(struct anv_physical_device *device)
  453. {
  454. anv_finish_wsi(device);
  455. anv_physical_device_free_disk_cache(device);
  456. ralloc_free(device->compiler);
  457. close(device->local_fd);
  458. if (device->master_fd >= 0)
  459. close(device->master_fd);
  460. }
  461. static void *
  462. default_alloc_func(void *pUserData, size_t size, size_t align,
  463. VkSystemAllocationScope allocationScope)
  464. {
  465. return malloc(size);
  466. }
  467. static void *
  468. default_realloc_func(void *pUserData, void *pOriginal, size_t size,
  469. size_t align, VkSystemAllocationScope allocationScope)
  470. {
  471. return realloc(pOriginal, size);
  472. }
  473. static void
  474. default_free_func(void *pUserData, void *pMemory)
  475. {
  476. free(pMemory);
  477. }
  478. static const VkAllocationCallbacks default_alloc = {
  479. .pUserData = NULL,
  480. .pfnAllocation = default_alloc_func,
  481. .pfnReallocation = default_realloc_func,
  482. .pfnFree = default_free_func,
  483. };
  484. VkResult anv_EnumerateInstanceExtensionProperties(
  485. const char* pLayerName,
  486. uint32_t* pPropertyCount,
  487. VkExtensionProperties* pProperties)
  488. {
  489. VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
  490. for (int i = 0; i < ANV_INSTANCE_EXTENSION_COUNT; i++) {
  491. if (anv_instance_extensions_supported.extensions[i]) {
  492. vk_outarray_append(&out, prop) {
  493. *prop = anv_instance_extensions[i];
  494. }
  495. }
  496. }
  497. return vk_outarray_status(&out);
  498. }
  499. VkResult anv_CreateInstance(
  500. const VkInstanceCreateInfo* pCreateInfo,
  501. const VkAllocationCallbacks* pAllocator,
  502. VkInstance* pInstance)
  503. {
  504. struct anv_instance *instance;
  505. VkResult result;
  506. assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO);
  507. struct anv_instance_extension_table enabled_extensions = {};
  508. for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
  509. int idx;
  510. for (idx = 0; idx < ANV_INSTANCE_EXTENSION_COUNT; idx++) {
  511. if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
  512. anv_instance_extensions[idx].extensionName) == 0)
  513. break;
  514. }
  515. if (idx >= ANV_INSTANCE_EXTENSION_COUNT)
  516. return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
  517. if (!anv_instance_extensions_supported.extensions[idx])
  518. return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
  519. enabled_extensions.extensions[idx] = true;
  520. }
  521. instance = vk_alloc2(&default_alloc, pAllocator, sizeof(*instance), 8,
  522. VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
  523. if (!instance)
  524. return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
  525. instance->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
  526. if (pAllocator)
  527. instance->alloc = *pAllocator;
  528. else
  529. instance->alloc = default_alloc;
  530. instance->app_info = (struct anv_app_info) { .api_version = 0 };
  531. if (pCreateInfo->pApplicationInfo) {
  532. const VkApplicationInfo *app = pCreateInfo->pApplicationInfo;
  533. instance->app_info.app_name =
  534. vk_strdup(&instance->alloc, app->pApplicationName,
  535. VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
  536. instance->app_info.app_version = app->applicationVersion;
  537. instance->app_info.engine_name =
  538. vk_strdup(&instance->alloc, app->pEngineName,
  539. VK_SYSTEM_ALLOCATION_SCOPE_INSTANCE);
  540. instance->app_info.engine_version = app->engineVersion;
  541. instance->app_info.api_version = app->apiVersion;
  542. }
  543. if (instance->app_info.api_version == 0)
  544. instance->app_info.api_version = VK_API_VERSION_1_0;
  545. instance->enabled_extensions = enabled_extensions;
  546. for (unsigned i = 0; i < ARRAY_SIZE(instance->dispatch.entrypoints); i++) {
  547. /* Vulkan requires that entrypoints for extensions which have not been
  548. * enabled must not be advertised.
  549. */
  550. if (!anv_instance_entrypoint_is_enabled(i, instance->app_info.api_version,
  551. &instance->enabled_extensions)) {
  552. instance->dispatch.entrypoints[i] = NULL;
  553. } else {
  554. instance->dispatch.entrypoints[i] =
  555. anv_instance_dispatch_table.entrypoints[i];
  556. }
  557. }
  558. for (unsigned i = 0; i < ARRAY_SIZE(instance->device_dispatch.entrypoints); i++) {
  559. /* Vulkan requires that entrypoints for extensions which have not been
  560. * enabled must not be advertised.
  561. */
  562. if (!anv_device_entrypoint_is_enabled(i, instance->app_info.api_version,
  563. &instance->enabled_extensions, NULL)) {
  564. instance->device_dispatch.entrypoints[i] = NULL;
  565. } else {
  566. instance->device_dispatch.entrypoints[i] =
  567. anv_device_dispatch_table.entrypoints[i];
  568. }
  569. }
  570. instance->physicalDeviceCount = -1;
  571. result = vk_debug_report_instance_init(&instance->debug_report_callbacks);
  572. if (result != VK_SUCCESS) {
  573. vk_free2(&default_alloc, pAllocator, instance);
  574. return vk_error(result);
  575. }
  576. instance->pipeline_cache_enabled =
  577. env_var_as_boolean("ANV_ENABLE_PIPELINE_CACHE", true);
  578. _mesa_locale_init();
  579. VG(VALGRIND_CREATE_MEMPOOL(instance, 0, false));
  580. *pInstance = anv_instance_to_handle(instance);
  581. return VK_SUCCESS;
  582. }
  583. void anv_DestroyInstance(
  584. VkInstance _instance,
  585. const VkAllocationCallbacks* pAllocator)
  586. {
  587. ANV_FROM_HANDLE(anv_instance, instance, _instance);
  588. if (!instance)
  589. return;
  590. if (instance->physicalDeviceCount > 0) {
  591. /* We support at most one physical device. */
  592. assert(instance->physicalDeviceCount == 1);
  593. anv_physical_device_finish(&instance->physicalDevice);
  594. }
  595. vk_free(&instance->alloc, (char *)instance->app_info.app_name);
  596. vk_free(&instance->alloc, (char *)instance->app_info.engine_name);
  597. VG(VALGRIND_DESTROY_MEMPOOL(instance));
  598. vk_debug_report_instance_destroy(&instance->debug_report_callbacks);
  599. _mesa_locale_fini();
  600. vk_free(&instance->alloc, instance);
  601. }
  602. static VkResult
  603. anv_enumerate_devices(struct anv_instance *instance)
  604. {
  605. /* TODO: Check for more devices ? */
  606. drmDevicePtr devices[8];
  607. VkResult result = VK_ERROR_INCOMPATIBLE_DRIVER;
  608. int max_devices;
  609. instance->physicalDeviceCount = 0;
  610. max_devices = drmGetDevices2(0, devices, ARRAY_SIZE(devices));
  611. if (max_devices < 1)
  612. return VK_ERROR_INCOMPATIBLE_DRIVER;
  613. for (unsigned i = 0; i < (unsigned)max_devices; i++) {
  614. if (devices[i]->available_nodes & 1 << DRM_NODE_RENDER &&
  615. devices[i]->bustype == DRM_BUS_PCI &&
  616. devices[i]->deviceinfo.pci->vendor_id == 0x8086) {
  617. result = anv_physical_device_init(&instance->physicalDevice,
  618. instance, devices[i]);
  619. if (result != VK_ERROR_INCOMPATIBLE_DRIVER)
  620. break;
  621. }
  622. }
  623. drmFreeDevices(devices, max_devices);
  624. if (result == VK_SUCCESS)
  625. instance->physicalDeviceCount = 1;
  626. return result;
  627. }
  628. static VkResult
  629. anv_instance_ensure_physical_device(struct anv_instance *instance)
  630. {
  631. if (instance->physicalDeviceCount < 0) {
  632. VkResult result = anv_enumerate_devices(instance);
  633. if (result != VK_SUCCESS &&
  634. result != VK_ERROR_INCOMPATIBLE_DRIVER)
  635. return result;
  636. }
  637. return VK_SUCCESS;
  638. }
  639. VkResult anv_EnumeratePhysicalDevices(
  640. VkInstance _instance,
  641. uint32_t* pPhysicalDeviceCount,
  642. VkPhysicalDevice* pPhysicalDevices)
  643. {
  644. ANV_FROM_HANDLE(anv_instance, instance, _instance);
  645. VK_OUTARRAY_MAKE(out, pPhysicalDevices, pPhysicalDeviceCount);
  646. VkResult result = anv_instance_ensure_physical_device(instance);
  647. if (result != VK_SUCCESS)
  648. return result;
  649. if (instance->physicalDeviceCount == 0)
  650. return VK_SUCCESS;
  651. assert(instance->physicalDeviceCount == 1);
  652. vk_outarray_append(&out, i) {
  653. *i = anv_physical_device_to_handle(&instance->physicalDevice);
  654. }
  655. return vk_outarray_status(&out);
  656. }
  657. VkResult anv_EnumeratePhysicalDeviceGroups(
  658. VkInstance _instance,
  659. uint32_t* pPhysicalDeviceGroupCount,
  660. VkPhysicalDeviceGroupProperties* pPhysicalDeviceGroupProperties)
  661. {
  662. ANV_FROM_HANDLE(anv_instance, instance, _instance);
  663. VK_OUTARRAY_MAKE(out, pPhysicalDeviceGroupProperties,
  664. pPhysicalDeviceGroupCount);
  665. VkResult result = anv_instance_ensure_physical_device(instance);
  666. if (result != VK_SUCCESS)
  667. return result;
  668. if (instance->physicalDeviceCount == 0)
  669. return VK_SUCCESS;
  670. assert(instance->physicalDeviceCount == 1);
  671. vk_outarray_append(&out, p) {
  672. p->physicalDeviceCount = 1;
  673. memset(p->physicalDevices, 0, sizeof(p->physicalDevices));
  674. p->physicalDevices[0] =
  675. anv_physical_device_to_handle(&instance->physicalDevice);
  676. p->subsetAllocation = VK_FALSE;
  677. vk_foreach_struct(ext, p->pNext)
  678. anv_debug_ignored_stype(ext->sType);
  679. }
  680. return vk_outarray_status(&out);
  681. }
  682. void anv_GetPhysicalDeviceFeatures(
  683. VkPhysicalDevice physicalDevice,
  684. VkPhysicalDeviceFeatures* pFeatures)
  685. {
  686. ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
  687. *pFeatures = (VkPhysicalDeviceFeatures) {
  688. .robustBufferAccess = true,
  689. .fullDrawIndexUint32 = true,
  690. .imageCubeArray = true,
  691. .independentBlend = true,
  692. .geometryShader = true,
  693. .tessellationShader = true,
  694. .sampleRateShading = true,
  695. .dualSrcBlend = true,
  696. .logicOp = true,
  697. .multiDrawIndirect = true,
  698. .drawIndirectFirstInstance = true,
  699. .depthClamp = true,
  700. .depthBiasClamp = true,
  701. .fillModeNonSolid = true,
  702. .depthBounds = false,
  703. .wideLines = true,
  704. .largePoints = true,
  705. .alphaToOne = true,
  706. .multiViewport = true,
  707. .samplerAnisotropy = true,
  708. .textureCompressionETC2 = pdevice->info.gen >= 8 ||
  709. pdevice->info.is_baytrail,
  710. .textureCompressionASTC_LDR = pdevice->info.gen >= 9, /* FINISHME CHV */
  711. .textureCompressionBC = true,
  712. .occlusionQueryPrecise = true,
  713. .pipelineStatisticsQuery = true,
  714. .fragmentStoresAndAtomics = true,
  715. .shaderTessellationAndGeometryPointSize = true,
  716. .shaderImageGatherExtended = true,
  717. .shaderStorageImageExtendedFormats = true,
  718. .shaderStorageImageMultisample = false,
  719. .shaderStorageImageReadWithoutFormat = false,
  720. .shaderStorageImageWriteWithoutFormat = true,
  721. .shaderUniformBufferArrayDynamicIndexing = true,
  722. .shaderSampledImageArrayDynamicIndexing = true,
  723. .shaderStorageBufferArrayDynamicIndexing = true,
  724. .shaderStorageImageArrayDynamicIndexing = true,
  725. .shaderClipDistance = true,
  726. .shaderCullDistance = true,
  727. .shaderFloat64 = pdevice->info.gen >= 8 &&
  728. pdevice->info.has_64bit_types,
  729. .shaderInt64 = pdevice->info.gen >= 8 &&
  730. pdevice->info.has_64bit_types,
  731. .shaderInt16 = pdevice->info.gen >= 8,
  732. .shaderResourceMinLod = pdevice->info.gen >= 9,
  733. .variableMultisampleRate = true,
  734. .inheritedQueries = true,
  735. };
  736. /* We can't do image stores in vec4 shaders */
  737. pFeatures->vertexPipelineStoresAndAtomics =
  738. pdevice->compiler->scalar_stage[MESA_SHADER_VERTEX] &&
  739. pdevice->compiler->scalar_stage[MESA_SHADER_GEOMETRY];
  740. struct anv_app_info *app_info = &pdevice->instance->app_info;
  741. /* The new DOOM and Wolfenstein games require depthBounds without
  742. * checking for it. They seem to run fine without it so just claim it's
  743. * there and accept the consequences.
  744. */
  745. if (app_info->engine_name && strcmp(app_info->engine_name, "idTech") == 0)
  746. pFeatures->depthBounds = true;
  747. }
  748. void anv_GetPhysicalDeviceFeatures2(
  749. VkPhysicalDevice physicalDevice,
  750. VkPhysicalDeviceFeatures2* pFeatures)
  751. {
  752. anv_GetPhysicalDeviceFeatures(physicalDevice, &pFeatures->features);
  753. vk_foreach_struct(ext, pFeatures->pNext) {
  754. switch (ext->sType) {
  755. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_8BIT_STORAGE_FEATURES_KHR: {
  756. VkPhysicalDevice8BitStorageFeaturesKHR *features =
  757. (VkPhysicalDevice8BitStorageFeaturesKHR *)ext;
  758. ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
  759. features->storageBuffer8BitAccess = pdevice->info.gen >= 8;
  760. features->uniformAndStorageBuffer8BitAccess = pdevice->info.gen >= 8;
  761. features->storagePushConstant8 = pdevice->info.gen >= 8;
  762. break;
  763. }
  764. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_16BIT_STORAGE_FEATURES: {
  765. VkPhysicalDevice16BitStorageFeatures *features =
  766. (VkPhysicalDevice16BitStorageFeatures *)ext;
  767. ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
  768. features->storageBuffer16BitAccess = pdevice->info.gen >= 8;
  769. features->uniformAndStorageBuffer16BitAccess = pdevice->info.gen >= 8;
  770. features->storagePushConstant16 = pdevice->info.gen >= 8;
  771. features->storageInputOutput16 = false;
  772. break;
  773. }
  774. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_FEATURES: {
  775. VkPhysicalDeviceMultiviewFeatures *features =
  776. (VkPhysicalDeviceMultiviewFeatures *)ext;
  777. features->multiview = true;
  778. features->multiviewGeometryShader = true;
  779. features->multiviewTessellationShader = true;
  780. break;
  781. }
  782. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_FEATURES: {
  783. VkPhysicalDeviceProtectedMemoryFeatures *features = (void *)ext;
  784. features->protectedMemory = VK_FALSE;
  785. break;
  786. }
  787. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_YCBCR_CONVERSION_FEATURES: {
  788. VkPhysicalDeviceSamplerYcbcrConversionFeatures *features =
  789. (VkPhysicalDeviceSamplerYcbcrConversionFeatures *) ext;
  790. features->samplerYcbcrConversion = true;
  791. break;
  792. }
  793. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SCALAR_BLOCK_LAYOUT_FEATURES_EXT: {
  794. VkPhysicalDeviceScalarBlockLayoutFeaturesEXT *features =
  795. (VkPhysicalDeviceScalarBlockLayoutFeaturesEXT *)ext;
  796. features->scalarBlockLayout = true;
  797. break;
  798. }
  799. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SHADER_DRAW_PARAMETER_FEATURES: {
  800. VkPhysicalDeviceShaderDrawParameterFeatures *features = (void *)ext;
  801. features->shaderDrawParameters = true;
  802. break;
  803. }
  804. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VARIABLE_POINTER_FEATURES: {
  805. VkPhysicalDeviceVariablePointerFeatures *features = (void *)ext;
  806. features->variablePointersStorageBuffer = true;
  807. features->variablePointers = true;
  808. break;
  809. }
  810. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_FEATURES_EXT: {
  811. VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *features =
  812. (VkPhysicalDeviceVertexAttributeDivisorFeaturesEXT *)ext;
  813. features->vertexAttributeInstanceRateDivisor = VK_TRUE;
  814. features->vertexAttributeInstanceRateZeroDivisor = VK_TRUE;
  815. break;
  816. }
  817. default:
  818. anv_debug_ignored_stype(ext->sType);
  819. break;
  820. }
  821. }
  822. }
  823. void anv_GetPhysicalDeviceProperties(
  824. VkPhysicalDevice physicalDevice,
  825. VkPhysicalDeviceProperties* pProperties)
  826. {
  827. ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
  828. const struct gen_device_info *devinfo = &pdevice->info;
  829. /* See assertions made when programming the buffer surface state. */
  830. const uint32_t max_raw_buffer_sz = devinfo->gen >= 7 ?
  831. (1ul << 30) : (1ul << 27);
  832. const uint32_t max_samplers = (devinfo->gen >= 8 || devinfo->is_haswell) ?
  833. 128 : 16;
  834. const uint32_t max_images = devinfo->gen < 9 ? MAX_GEN8_IMAGES : MAX_IMAGES;
  835. VkSampleCountFlags sample_counts =
  836. isl_device_get_sample_counts(&pdevice->isl_dev);
  837. VkPhysicalDeviceLimits limits = {
  838. .maxImageDimension1D = (1 << 14),
  839. .maxImageDimension2D = (1 << 14),
  840. .maxImageDimension3D = (1 << 11),
  841. .maxImageDimensionCube = (1 << 14),
  842. .maxImageArrayLayers = (1 << 11),
  843. .maxTexelBufferElements = 128 * 1024 * 1024,
  844. .maxUniformBufferRange = (1ul << 27),
  845. .maxStorageBufferRange = max_raw_buffer_sz,
  846. .maxPushConstantsSize = MAX_PUSH_CONSTANTS_SIZE,
  847. .maxMemoryAllocationCount = UINT32_MAX,
  848. .maxSamplerAllocationCount = 64 * 1024,
  849. .bufferImageGranularity = 64, /* A cache line */
  850. .sparseAddressSpaceSize = 0,
  851. .maxBoundDescriptorSets = MAX_SETS,
  852. .maxPerStageDescriptorSamplers = max_samplers,
  853. .maxPerStageDescriptorUniformBuffers = 64,
  854. .maxPerStageDescriptorStorageBuffers = 64,
  855. .maxPerStageDescriptorSampledImages = max_samplers,
  856. .maxPerStageDescriptorStorageImages = max_images,
  857. .maxPerStageDescriptorInputAttachments = 64,
  858. .maxPerStageResources = 250,
  859. .maxDescriptorSetSamplers = 6 * max_samplers, /* number of stages * maxPerStageDescriptorSamplers */
  860. .maxDescriptorSetUniformBuffers = 6 * 64, /* number of stages * maxPerStageDescriptorUniformBuffers */
  861. .maxDescriptorSetUniformBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2,
  862. .maxDescriptorSetStorageBuffers = 6 * 64, /* number of stages * maxPerStageDescriptorStorageBuffers */
  863. .maxDescriptorSetStorageBuffersDynamic = MAX_DYNAMIC_BUFFERS / 2,
  864. .maxDescriptorSetSampledImages = 6 * max_samplers, /* number of stages * maxPerStageDescriptorSampledImages */
  865. .maxDescriptorSetStorageImages = 6 * max_images, /* number of stages * maxPerStageDescriptorStorageImages */
  866. .maxDescriptorSetInputAttachments = 256,
  867. .maxVertexInputAttributes = MAX_VBS,
  868. .maxVertexInputBindings = MAX_VBS,
  869. .maxVertexInputAttributeOffset = 2047,
  870. .maxVertexInputBindingStride = 2048,
  871. .maxVertexOutputComponents = 128,
  872. .maxTessellationGenerationLevel = 64,
  873. .maxTessellationPatchSize = 32,
  874. .maxTessellationControlPerVertexInputComponents = 128,
  875. .maxTessellationControlPerVertexOutputComponents = 128,
  876. .maxTessellationControlPerPatchOutputComponents = 128,
  877. .maxTessellationControlTotalOutputComponents = 2048,
  878. .maxTessellationEvaluationInputComponents = 128,
  879. .maxTessellationEvaluationOutputComponents = 128,
  880. .maxGeometryShaderInvocations = 32,
  881. .maxGeometryInputComponents = 64,
  882. .maxGeometryOutputComponents = 128,
  883. .maxGeometryOutputVertices = 256,
  884. .maxGeometryTotalOutputComponents = 1024,
  885. .maxFragmentInputComponents = 112, /* 128 components - (POS, PSIZ, CLIP_DIST0, CLIP_DIST1) */
  886. .maxFragmentOutputAttachments = 8,
  887. .maxFragmentDualSrcAttachments = 1,
  888. .maxFragmentCombinedOutputResources = 8,
  889. .maxComputeSharedMemorySize = 32768,
  890. .maxComputeWorkGroupCount = { 65535, 65535, 65535 },
  891. .maxComputeWorkGroupInvocations = 16 * devinfo->max_cs_threads,
  892. .maxComputeWorkGroupSize = {
  893. 16 * devinfo->max_cs_threads,
  894. 16 * devinfo->max_cs_threads,
  895. 16 * devinfo->max_cs_threads,
  896. },
  897. .subPixelPrecisionBits = 4 /* FIXME */,
  898. .subTexelPrecisionBits = 4 /* FIXME */,
  899. .mipmapPrecisionBits = 4 /* FIXME */,
  900. .maxDrawIndexedIndexValue = UINT32_MAX,
  901. .maxDrawIndirectCount = UINT32_MAX,
  902. .maxSamplerLodBias = 16,
  903. .maxSamplerAnisotropy = 16,
  904. .maxViewports = MAX_VIEWPORTS,
  905. .maxViewportDimensions = { (1 << 14), (1 << 14) },
  906. .viewportBoundsRange = { INT16_MIN, INT16_MAX },
  907. .viewportSubPixelBits = 13, /* We take a float? */
  908. .minMemoryMapAlignment = 4096, /* A page */
  909. .minTexelBufferOffsetAlignment = 1,
  910. /* We need 16 for UBO block reads to work and 32 for push UBOs */
  911. .minUniformBufferOffsetAlignment = 32,
  912. .minStorageBufferOffsetAlignment = 4,
  913. .minTexelOffset = -8,
  914. .maxTexelOffset = 7,
  915. .minTexelGatherOffset = -32,
  916. .maxTexelGatherOffset = 31,
  917. .minInterpolationOffset = -0.5,
  918. .maxInterpolationOffset = 0.4375,
  919. .subPixelInterpolationOffsetBits = 4,
  920. .maxFramebufferWidth = (1 << 14),
  921. .maxFramebufferHeight = (1 << 14),
  922. .maxFramebufferLayers = (1 << 11),
  923. .framebufferColorSampleCounts = sample_counts,
  924. .framebufferDepthSampleCounts = sample_counts,
  925. .framebufferStencilSampleCounts = sample_counts,
  926. .framebufferNoAttachmentsSampleCounts = sample_counts,
  927. .maxColorAttachments = MAX_RTS,
  928. .sampledImageColorSampleCounts = sample_counts,
  929. .sampledImageIntegerSampleCounts = VK_SAMPLE_COUNT_1_BIT,
  930. .sampledImageDepthSampleCounts = sample_counts,
  931. .sampledImageStencilSampleCounts = sample_counts,
  932. .storageImageSampleCounts = VK_SAMPLE_COUNT_1_BIT,
  933. .maxSampleMaskWords = 1,
  934. .timestampComputeAndGraphics = false,
  935. .timestampPeriod = 1000000000.0 / devinfo->timestamp_frequency,
  936. .maxClipDistances = 8,
  937. .maxCullDistances = 8,
  938. .maxCombinedClipAndCullDistances = 8,
  939. .discreteQueuePriorities = 2,
  940. .pointSizeRange = { 0.125, 255.875 },
  941. .lineWidthRange = { 0.0, 7.9921875 },
  942. .pointSizeGranularity = (1.0 / 8.0),
  943. .lineWidthGranularity = (1.0 / 128.0),
  944. .strictLines = false, /* FINISHME */
  945. .standardSampleLocations = true,
  946. .optimalBufferCopyOffsetAlignment = 128,
  947. .optimalBufferCopyRowPitchAlignment = 128,
  948. .nonCoherentAtomSize = 64,
  949. };
  950. *pProperties = (VkPhysicalDeviceProperties) {
  951. .apiVersion = anv_physical_device_api_version(pdevice),
  952. .driverVersion = vk_get_driver_version(),
  953. .vendorID = 0x8086,
  954. .deviceID = pdevice->chipset_id,
  955. .deviceType = VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU,
  956. .limits = limits,
  957. .sparseProperties = {0}, /* Broadwell doesn't do sparse. */
  958. };
  959. snprintf(pProperties->deviceName, sizeof(pProperties->deviceName),
  960. "%s", pdevice->name);
  961. memcpy(pProperties->pipelineCacheUUID,
  962. pdevice->pipeline_cache_uuid, VK_UUID_SIZE);
  963. }
  964. void anv_GetPhysicalDeviceProperties2(
  965. VkPhysicalDevice physicalDevice,
  966. VkPhysicalDeviceProperties2* pProperties)
  967. {
  968. ANV_FROM_HANDLE(anv_physical_device, pdevice, physicalDevice);
  969. anv_GetPhysicalDeviceProperties(physicalDevice, &pProperties->properties);
  970. vk_foreach_struct(ext, pProperties->pNext) {
  971. switch (ext->sType) {
  972. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_STENCIL_RESOLVE_PROPERTIES_KHR: {
  973. VkPhysicalDeviceDepthStencilResolvePropertiesKHR *props =
  974. (VkPhysicalDeviceDepthStencilResolvePropertiesKHR *)ext;
  975. /* We support all of the depth resolve modes */
  976. props->supportedDepthResolveModes =
  977. VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR |
  978. VK_RESOLVE_MODE_AVERAGE_BIT_KHR |
  979. VK_RESOLVE_MODE_MIN_BIT_KHR |
  980. VK_RESOLVE_MODE_MAX_BIT_KHR;
  981. /* Average doesn't make sense for stencil so we don't support that */
  982. props->supportedStencilResolveModes =
  983. VK_RESOLVE_MODE_SAMPLE_ZERO_BIT_KHR;
  984. if (pdevice->info.gen >= 8) {
  985. /* The advanced stencil resolve modes currently require stencil
  986. * sampling be supported by the hardware.
  987. */
  988. props->supportedStencilResolveModes |=
  989. VK_RESOLVE_MODE_MIN_BIT_KHR |
  990. VK_RESOLVE_MODE_MAX_BIT_KHR;
  991. }
  992. props->independentResolveNone = VK_TRUE;
  993. props->independentResolve = VK_TRUE;
  994. break;
  995. }
  996. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DRIVER_PROPERTIES_KHR: {
  997. VkPhysicalDeviceDriverPropertiesKHR *driver_props =
  998. (VkPhysicalDeviceDriverPropertiesKHR *) ext;
  999. driver_props->driverID = VK_DRIVER_ID_INTEL_OPEN_SOURCE_MESA_KHR;
  1000. util_snprintf(driver_props->driverName, VK_MAX_DRIVER_NAME_SIZE_KHR,
  1001. "Intel open-source Mesa driver");
  1002. util_snprintf(driver_props->driverInfo, VK_MAX_DRIVER_INFO_SIZE_KHR,
  1003. "Mesa " PACKAGE_VERSION MESA_GIT_SHA1);
  1004. driver_props->conformanceVersion = (VkConformanceVersionKHR) {
  1005. .major = 1,
  1006. .minor = 1,
  1007. .subminor = 2,
  1008. .patch = 0,
  1009. };
  1010. break;
  1011. }
  1012. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_ID_PROPERTIES: {
  1013. VkPhysicalDeviceIDProperties *id_props =
  1014. (VkPhysicalDeviceIDProperties *)ext;
  1015. memcpy(id_props->deviceUUID, pdevice->device_uuid, VK_UUID_SIZE);
  1016. memcpy(id_props->driverUUID, pdevice->driver_uuid, VK_UUID_SIZE);
  1017. /* The LUID is for Windows. */
  1018. id_props->deviceLUIDValid = false;
  1019. break;
  1020. }
  1021. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MAINTENANCE_3_PROPERTIES: {
  1022. VkPhysicalDeviceMaintenance3Properties *props =
  1023. (VkPhysicalDeviceMaintenance3Properties *)ext;
  1024. /* This value doesn't matter for us today as our per-stage
  1025. * descriptors are the real limit.
  1026. */
  1027. props->maxPerSetDescriptors = 1024;
  1028. props->maxMemoryAllocationSize = MAX_MEMORY_ALLOCATION_SIZE;
  1029. break;
  1030. }
  1031. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MULTIVIEW_PROPERTIES: {
  1032. VkPhysicalDeviceMultiviewProperties *properties =
  1033. (VkPhysicalDeviceMultiviewProperties *)ext;
  1034. properties->maxMultiviewViewCount = 16;
  1035. properties->maxMultiviewInstanceIndex = UINT32_MAX / 16;
  1036. break;
  1037. }
  1038. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PCI_BUS_INFO_PROPERTIES_EXT: {
  1039. VkPhysicalDevicePCIBusInfoPropertiesEXT *properties =
  1040. (VkPhysicalDevicePCIBusInfoPropertiesEXT *)ext;
  1041. properties->pciDomain = pdevice->pci_info.domain;
  1042. properties->pciBus = pdevice->pci_info.bus;
  1043. properties->pciDevice = pdevice->pci_info.device;
  1044. properties->pciFunction = pdevice->pci_info.function;
  1045. break;
  1046. }
  1047. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES: {
  1048. VkPhysicalDevicePointClippingProperties *properties =
  1049. (VkPhysicalDevicePointClippingProperties *) ext;
  1050. properties->pointClippingBehavior = VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES;
  1051. anv_finishme("Implement pop-free point clipping");
  1052. break;
  1053. }
  1054. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROTECTED_MEMORY_PROPERTIES: {
  1055. VkPhysicalDeviceProtectedMemoryProperties *props =
  1056. (VkPhysicalDeviceProtectedMemoryProperties *)ext;
  1057. props->protectedNoFault = false;
  1058. break;
  1059. }
  1060. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PUSH_DESCRIPTOR_PROPERTIES_KHR: {
  1061. VkPhysicalDevicePushDescriptorPropertiesKHR *properties =
  1062. (VkPhysicalDevicePushDescriptorPropertiesKHR *) ext;
  1063. properties->maxPushDescriptors = MAX_PUSH_DESCRIPTORS;
  1064. break;
  1065. }
  1066. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SAMPLER_FILTER_MINMAX_PROPERTIES_EXT: {
  1067. VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT *properties =
  1068. (VkPhysicalDeviceSamplerFilterMinmaxPropertiesEXT *)ext;
  1069. properties->filterMinmaxImageComponentMapping = pdevice->info.gen >= 9;
  1070. properties->filterMinmaxSingleComponentFormats = true;
  1071. break;
  1072. }
  1073. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES: {
  1074. VkPhysicalDeviceSubgroupProperties *properties = (void *)ext;
  1075. properties->subgroupSize = BRW_SUBGROUP_SIZE;
  1076. VkShaderStageFlags scalar_stages = 0;
  1077. for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
  1078. if (pdevice->compiler->scalar_stage[stage])
  1079. scalar_stages |= mesa_to_vk_shader_stage(stage);
  1080. }
  1081. properties->supportedStages = scalar_stages;
  1082. properties->supportedOperations = VK_SUBGROUP_FEATURE_BASIC_BIT |
  1083. VK_SUBGROUP_FEATURE_VOTE_BIT |
  1084. VK_SUBGROUP_FEATURE_ARITHMETIC_BIT |
  1085. VK_SUBGROUP_FEATURE_BALLOT_BIT |
  1086. VK_SUBGROUP_FEATURE_SHUFFLE_BIT |
  1087. VK_SUBGROUP_FEATURE_SHUFFLE_RELATIVE_BIT |
  1088. VK_SUBGROUP_FEATURE_CLUSTERED_BIT |
  1089. VK_SUBGROUP_FEATURE_QUAD_BIT;
  1090. properties->quadOperationsInAllStages = VK_TRUE;
  1091. break;
  1092. }
  1093. case VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_ATTRIBUTE_DIVISOR_PROPERTIES_EXT: {
  1094. VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *props =
  1095. (VkPhysicalDeviceVertexAttributeDivisorPropertiesEXT *)ext;
  1096. /* We have to restrict this a bit for multiview */
  1097. props->maxVertexAttribDivisor = UINT32_MAX / 16;
  1098. break;
  1099. }
  1100. default:
  1101. anv_debug_ignored_stype(ext->sType);
  1102. break;
  1103. }
  1104. }
  1105. }
  1106. /* We support exactly one queue family. */
  1107. static const VkQueueFamilyProperties
  1108. anv_queue_family_properties = {
  1109. .queueFlags = VK_QUEUE_GRAPHICS_BIT |
  1110. VK_QUEUE_COMPUTE_BIT |
  1111. VK_QUEUE_TRANSFER_BIT,
  1112. .queueCount = 1,
  1113. .timestampValidBits = 36, /* XXX: Real value here */
  1114. .minImageTransferGranularity = { 1, 1, 1 },
  1115. };
  1116. void anv_GetPhysicalDeviceQueueFamilyProperties(
  1117. VkPhysicalDevice physicalDevice,
  1118. uint32_t* pCount,
  1119. VkQueueFamilyProperties* pQueueFamilyProperties)
  1120. {
  1121. VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pCount);
  1122. vk_outarray_append(&out, p) {
  1123. *p = anv_queue_family_properties;
  1124. }
  1125. }
  1126. void anv_GetPhysicalDeviceQueueFamilyProperties2(
  1127. VkPhysicalDevice physicalDevice,
  1128. uint32_t* pQueueFamilyPropertyCount,
  1129. VkQueueFamilyProperties2* pQueueFamilyProperties)
  1130. {
  1131. VK_OUTARRAY_MAKE(out, pQueueFamilyProperties, pQueueFamilyPropertyCount);
  1132. vk_outarray_append(&out, p) {
  1133. p->queueFamilyProperties = anv_queue_family_properties;
  1134. vk_foreach_struct(s, p->pNext) {
  1135. anv_debug_ignored_stype(s->sType);
  1136. }
  1137. }
  1138. }
  1139. void anv_GetPhysicalDeviceMemoryProperties(
  1140. VkPhysicalDevice physicalDevice,
  1141. VkPhysicalDeviceMemoryProperties* pMemoryProperties)
  1142. {
  1143. ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
  1144. pMemoryProperties->memoryTypeCount = physical_device->memory.type_count;
  1145. for (uint32_t i = 0; i < physical_device->memory.type_count; i++) {
  1146. pMemoryProperties->memoryTypes[i] = (VkMemoryType) {
  1147. .propertyFlags = physical_device->memory.types[i].propertyFlags,
  1148. .heapIndex = physical_device->memory.types[i].heapIndex,
  1149. };
  1150. }
  1151. pMemoryProperties->memoryHeapCount = physical_device->memory.heap_count;
  1152. for (uint32_t i = 0; i < physical_device->memory.heap_count; i++) {
  1153. pMemoryProperties->memoryHeaps[i] = (VkMemoryHeap) {
  1154. .size = physical_device->memory.heaps[i].size,
  1155. .flags = physical_device->memory.heaps[i].flags,
  1156. };
  1157. }
  1158. }
  1159. void anv_GetPhysicalDeviceMemoryProperties2(
  1160. VkPhysicalDevice physicalDevice,
  1161. VkPhysicalDeviceMemoryProperties2* pMemoryProperties)
  1162. {
  1163. anv_GetPhysicalDeviceMemoryProperties(physicalDevice,
  1164. &pMemoryProperties->memoryProperties);
  1165. vk_foreach_struct(ext, pMemoryProperties->pNext) {
  1166. switch (ext->sType) {
  1167. default:
  1168. anv_debug_ignored_stype(ext->sType);
  1169. break;
  1170. }
  1171. }
  1172. }
  1173. void
  1174. anv_GetDeviceGroupPeerMemoryFeatures(
  1175. VkDevice device,
  1176. uint32_t heapIndex,
  1177. uint32_t localDeviceIndex,
  1178. uint32_t remoteDeviceIndex,
  1179. VkPeerMemoryFeatureFlags* pPeerMemoryFeatures)
  1180. {
  1181. assert(localDeviceIndex == 0 && remoteDeviceIndex == 0);
  1182. *pPeerMemoryFeatures = VK_PEER_MEMORY_FEATURE_COPY_SRC_BIT |
  1183. VK_PEER_MEMORY_FEATURE_COPY_DST_BIT |
  1184. VK_PEER_MEMORY_FEATURE_GENERIC_SRC_BIT |
  1185. VK_PEER_MEMORY_FEATURE_GENERIC_DST_BIT;
  1186. }
  1187. PFN_vkVoidFunction anv_GetInstanceProcAddr(
  1188. VkInstance _instance,
  1189. const char* pName)
  1190. {
  1191. ANV_FROM_HANDLE(anv_instance, instance, _instance);
  1192. /* The Vulkan 1.0 spec for vkGetInstanceProcAddr has a table of exactly
  1193. * when we have to return valid function pointers, NULL, or it's left
  1194. * undefined. See the table for exact details.
  1195. */
  1196. if (pName == NULL)
  1197. return NULL;
  1198. #define LOOKUP_ANV_ENTRYPOINT(entrypoint) \
  1199. if (strcmp(pName, "vk" #entrypoint) == 0) \
  1200. return (PFN_vkVoidFunction)anv_##entrypoint
  1201. LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceExtensionProperties);
  1202. LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceLayerProperties);
  1203. LOOKUP_ANV_ENTRYPOINT(EnumerateInstanceVersion);
  1204. LOOKUP_ANV_ENTRYPOINT(CreateInstance);
  1205. #undef LOOKUP_ANV_ENTRYPOINT
  1206. if (instance == NULL)
  1207. return NULL;
  1208. int idx = anv_get_instance_entrypoint_index(pName);
  1209. if (idx >= 0)
  1210. return instance->dispatch.entrypoints[idx];
  1211. idx = anv_get_device_entrypoint_index(pName);
  1212. if (idx >= 0)
  1213. return instance->device_dispatch.entrypoints[idx];
  1214. return NULL;
  1215. }
  1216. /* With version 1+ of the loader interface the ICD should expose
  1217. * vk_icdGetInstanceProcAddr to work around certain LD_PRELOAD issues seen in apps.
  1218. */
  1219. PUBLIC
  1220. VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
  1221. VkInstance instance,
  1222. const char* pName);
  1223. PUBLIC
  1224. VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vk_icdGetInstanceProcAddr(
  1225. VkInstance instance,
  1226. const char* pName)
  1227. {
  1228. return anv_GetInstanceProcAddr(instance, pName);
  1229. }
  1230. PFN_vkVoidFunction anv_GetDeviceProcAddr(
  1231. VkDevice _device,
  1232. const char* pName)
  1233. {
  1234. ANV_FROM_HANDLE(anv_device, device, _device);
  1235. if (!device || !pName)
  1236. return NULL;
  1237. int idx = anv_get_device_entrypoint_index(pName);
  1238. if (idx < 0)
  1239. return NULL;
  1240. return device->dispatch.entrypoints[idx];
  1241. }
  1242. VkResult
  1243. anv_CreateDebugReportCallbackEXT(VkInstance _instance,
  1244. const VkDebugReportCallbackCreateInfoEXT* pCreateInfo,
  1245. const VkAllocationCallbacks* pAllocator,
  1246. VkDebugReportCallbackEXT* pCallback)
  1247. {
  1248. ANV_FROM_HANDLE(anv_instance, instance, _instance);
  1249. return vk_create_debug_report_callback(&instance->debug_report_callbacks,
  1250. pCreateInfo, pAllocator, &instance->alloc,
  1251. pCallback);
  1252. }
  1253. void
  1254. anv_DestroyDebugReportCallbackEXT(VkInstance _instance,
  1255. VkDebugReportCallbackEXT _callback,
  1256. const VkAllocationCallbacks* pAllocator)
  1257. {
  1258. ANV_FROM_HANDLE(anv_instance, instance, _instance);
  1259. vk_destroy_debug_report_callback(&instance->debug_report_callbacks,
  1260. _callback, pAllocator, &instance->alloc);
  1261. }
  1262. void
  1263. anv_DebugReportMessageEXT(VkInstance _instance,
  1264. VkDebugReportFlagsEXT flags,
  1265. VkDebugReportObjectTypeEXT objectType,
  1266. uint64_t object,
  1267. size_t location,
  1268. int32_t messageCode,
  1269. const char* pLayerPrefix,
  1270. const char* pMessage)
  1271. {
  1272. ANV_FROM_HANDLE(anv_instance, instance, _instance);
  1273. vk_debug_report(&instance->debug_report_callbacks, flags, objectType,
  1274. object, location, messageCode, pLayerPrefix, pMessage);
  1275. }
  1276. static void
  1277. anv_queue_init(struct anv_device *device, struct anv_queue *queue)
  1278. {
  1279. queue->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
  1280. queue->device = device;
  1281. queue->flags = 0;
  1282. }
  1283. static void
  1284. anv_queue_finish(struct anv_queue *queue)
  1285. {
  1286. }
  1287. static struct anv_state
  1288. anv_state_pool_emit_data(struct anv_state_pool *pool, size_t size, size_t align, const void *p)
  1289. {
  1290. struct anv_state state;
  1291. state = anv_state_pool_alloc(pool, size, align);
  1292. memcpy(state.map, p, size);
  1293. return state;
  1294. }
  1295. struct gen8_border_color {
  1296. union {
  1297. float float32[4];
  1298. uint32_t uint32[4];
  1299. };
  1300. /* Pad out to 64 bytes */
  1301. uint32_t _pad[12];
  1302. };
  1303. static void
  1304. anv_device_init_border_colors(struct anv_device *device)
  1305. {
  1306. static const struct gen8_border_color border_colors[] = {
  1307. [VK_BORDER_COLOR_FLOAT_TRANSPARENT_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 0.0 } },
  1308. [VK_BORDER_COLOR_FLOAT_OPAQUE_BLACK] = { .float32 = { 0.0, 0.0, 0.0, 1.0 } },
  1309. [VK_BORDER_COLOR_FLOAT_OPAQUE_WHITE] = { .float32 = { 1.0, 1.0, 1.0, 1.0 } },
  1310. [VK_BORDER_COLOR_INT_TRANSPARENT_BLACK] = { .uint32 = { 0, 0, 0, 0 } },
  1311. [VK_BORDER_COLOR_INT_OPAQUE_BLACK] = { .uint32 = { 0, 0, 0, 1 } },
  1312. [VK_BORDER_COLOR_INT_OPAQUE_WHITE] = { .uint32 = { 1, 1, 1, 1 } },
  1313. };
  1314. device->border_colors = anv_state_pool_emit_data(&device->dynamic_state_pool,
  1315. sizeof(border_colors), 64,
  1316. border_colors);
  1317. }
  1318. static void
  1319. anv_device_init_trivial_batch(struct anv_device *device)
  1320. {
  1321. anv_bo_init_new(&device->trivial_batch_bo, device, 4096);
  1322. if (device->instance->physicalDevice.has_exec_async)
  1323. device->trivial_batch_bo.flags |= EXEC_OBJECT_ASYNC;
  1324. if (device->instance->physicalDevice.use_softpin)
  1325. device->trivial_batch_bo.flags |= EXEC_OBJECT_PINNED;
  1326. anv_vma_alloc(device, &device->trivial_batch_bo);
  1327. void *map = anv_gem_mmap(device, device->trivial_batch_bo.gem_handle,
  1328. 0, 4096, 0);
  1329. struct anv_batch batch = {
  1330. .start = map,
  1331. .next = map,
  1332. .end = map + 4096,
  1333. };
  1334. anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
  1335. anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
  1336. if (!device->info.has_llc)
  1337. gen_clflush_range(map, batch.next - map);
  1338. anv_gem_munmap(map, device->trivial_batch_bo.size);
  1339. }
  1340. VkResult anv_EnumerateDeviceExtensionProperties(
  1341. VkPhysicalDevice physicalDevice,
  1342. const char* pLayerName,
  1343. uint32_t* pPropertyCount,
  1344. VkExtensionProperties* pProperties)
  1345. {
  1346. ANV_FROM_HANDLE(anv_physical_device, device, physicalDevice);
  1347. VK_OUTARRAY_MAKE(out, pProperties, pPropertyCount);
  1348. for (int i = 0; i < ANV_DEVICE_EXTENSION_COUNT; i++) {
  1349. if (device->supported_extensions.extensions[i]) {
  1350. vk_outarray_append(&out, prop) {
  1351. *prop = anv_device_extensions[i];
  1352. }
  1353. }
  1354. }
  1355. return vk_outarray_status(&out);
  1356. }
  1357. static void
  1358. anv_device_init_dispatch(struct anv_device *device)
  1359. {
  1360. const struct anv_device_dispatch_table *genX_table;
  1361. switch (device->info.gen) {
  1362. case 11:
  1363. genX_table = &gen11_device_dispatch_table;
  1364. break;
  1365. case 10:
  1366. genX_table = &gen10_device_dispatch_table;
  1367. break;
  1368. case 9:
  1369. genX_table = &gen9_device_dispatch_table;
  1370. break;
  1371. case 8:
  1372. genX_table = &gen8_device_dispatch_table;
  1373. break;
  1374. case 7:
  1375. if (device->info.is_haswell)
  1376. genX_table = &gen75_device_dispatch_table;
  1377. else
  1378. genX_table = &gen7_device_dispatch_table;
  1379. break;
  1380. default:
  1381. unreachable("unsupported gen\n");
  1382. }
  1383. for (unsigned i = 0; i < ARRAY_SIZE(device->dispatch.entrypoints); i++) {
  1384. /* Vulkan requires that entrypoints for extensions which have not been
  1385. * enabled must not be advertised.
  1386. */
  1387. if (!anv_device_entrypoint_is_enabled(i, device->instance->app_info.api_version,
  1388. &device->instance->enabled_extensions,
  1389. &device->enabled_extensions)) {
  1390. device->dispatch.entrypoints[i] = NULL;
  1391. } else if (genX_table->entrypoints[i]) {
  1392. device->dispatch.entrypoints[i] = genX_table->entrypoints[i];
  1393. } else {
  1394. device->dispatch.entrypoints[i] =
  1395. anv_device_dispatch_table.entrypoints[i];
  1396. }
  1397. }
  1398. }
  1399. static int
  1400. vk_priority_to_gen(int priority)
  1401. {
  1402. switch (priority) {
  1403. case VK_QUEUE_GLOBAL_PRIORITY_LOW_EXT:
  1404. return GEN_CONTEXT_LOW_PRIORITY;
  1405. case VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT:
  1406. return GEN_CONTEXT_MEDIUM_PRIORITY;
  1407. case VK_QUEUE_GLOBAL_PRIORITY_HIGH_EXT:
  1408. return GEN_CONTEXT_HIGH_PRIORITY;
  1409. case VK_QUEUE_GLOBAL_PRIORITY_REALTIME_EXT:
  1410. return GEN_CONTEXT_REALTIME_PRIORITY;
  1411. default:
  1412. unreachable("Invalid priority");
  1413. }
  1414. }
  1415. static void
  1416. anv_device_init_hiz_clear_value_bo(struct anv_device *device)
  1417. {
  1418. anv_bo_init_new(&device->hiz_clear_bo, device, 4096);
  1419. if (device->instance->physicalDevice.has_exec_async)
  1420. device->hiz_clear_bo.flags |= EXEC_OBJECT_ASYNC;
  1421. if (device->instance->physicalDevice.use_softpin)
  1422. device->hiz_clear_bo.flags |= EXEC_OBJECT_PINNED;
  1423. anv_vma_alloc(device, &device->hiz_clear_bo);
  1424. uint32_t *map = anv_gem_mmap(device, device->hiz_clear_bo.gem_handle,
  1425. 0, 4096, 0);
  1426. union isl_color_value hiz_clear = { .u32 = { 0, } };
  1427. hiz_clear.f32[0] = ANV_HZ_FC_VAL;
  1428. memcpy(map, hiz_clear.u32, sizeof(hiz_clear.u32));
  1429. anv_gem_munmap(map, device->hiz_clear_bo.size);
  1430. }
  1431. VkResult anv_CreateDevice(
  1432. VkPhysicalDevice physicalDevice,
  1433. const VkDeviceCreateInfo* pCreateInfo,
  1434. const VkAllocationCallbacks* pAllocator,
  1435. VkDevice* pDevice)
  1436. {
  1437. ANV_FROM_HANDLE(anv_physical_device, physical_device, physicalDevice);
  1438. VkResult result;
  1439. struct anv_device *device;
  1440. assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
  1441. struct anv_device_extension_table enabled_extensions = { };
  1442. for (uint32_t i = 0; i < pCreateInfo->enabledExtensionCount; i++) {
  1443. int idx;
  1444. for (idx = 0; idx < ANV_DEVICE_EXTENSION_COUNT; idx++) {
  1445. if (strcmp(pCreateInfo->ppEnabledExtensionNames[i],
  1446. anv_device_extensions[idx].extensionName) == 0)
  1447. break;
  1448. }
  1449. if (idx >= ANV_DEVICE_EXTENSION_COUNT)
  1450. return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
  1451. if (!physical_device->supported_extensions.extensions[idx])
  1452. return vk_error(VK_ERROR_EXTENSION_NOT_PRESENT);
  1453. enabled_extensions.extensions[idx] = true;
  1454. }
  1455. /* Check enabled features */
  1456. if (pCreateInfo->pEnabledFeatures) {
  1457. VkPhysicalDeviceFeatures supported_features;
  1458. anv_GetPhysicalDeviceFeatures(physicalDevice, &supported_features);
  1459. VkBool32 *supported_feature = (VkBool32 *)&supported_features;
  1460. VkBool32 *enabled_feature = (VkBool32 *)pCreateInfo->pEnabledFeatures;
  1461. unsigned num_features = sizeof(VkPhysicalDeviceFeatures) / sizeof(VkBool32);
  1462. for (uint32_t i = 0; i < num_features; i++) {
  1463. if (enabled_feature[i] && !supported_feature[i])
  1464. return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
  1465. }
  1466. }
  1467. /* Check requested queues and fail if we are requested to create any
  1468. * queues with flags we don't support.
  1469. */
  1470. assert(pCreateInfo->queueCreateInfoCount > 0);
  1471. for (uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++) {
  1472. if (pCreateInfo->pQueueCreateInfos[i].flags != 0)
  1473. return vk_error(VK_ERROR_INITIALIZATION_FAILED);
  1474. }
  1475. /* Check if client specified queue priority. */
  1476. const VkDeviceQueueGlobalPriorityCreateInfoEXT *queue_priority =
  1477. vk_find_struct_const(pCreateInfo->pQueueCreateInfos[0].pNext,
  1478. DEVICE_QUEUE_GLOBAL_PRIORITY_CREATE_INFO_EXT);
  1479. VkQueueGlobalPriorityEXT priority =
  1480. queue_priority ? queue_priority->globalPriority :
  1481. VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT;
  1482. device = vk_alloc2(&physical_device->instance->alloc, pAllocator,
  1483. sizeof(*device), 8,
  1484. VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
  1485. if (!device)
  1486. return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
  1487. device->_loader_data.loaderMagic = ICD_LOADER_MAGIC;
  1488. device->instance = physical_device->instance;
  1489. device->chipset_id = physical_device->chipset_id;
  1490. device->no_hw = physical_device->no_hw;
  1491. device->_lost = false;
  1492. if (pAllocator)
  1493. device->alloc = *pAllocator;
  1494. else
  1495. device->alloc = physical_device->instance->alloc;
  1496. /* XXX(chadv): Can we dup() physicalDevice->fd here? */
  1497. device->fd = open(physical_device->path, O_RDWR | O_CLOEXEC);
  1498. if (device->fd == -1) {
  1499. result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
  1500. goto fail_device;
  1501. }
  1502. device->context_id = anv_gem_create_context(device);
  1503. if (device->context_id == -1) {
  1504. result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
  1505. goto fail_fd;
  1506. }
  1507. if (physical_device->use_softpin) {
  1508. if (pthread_mutex_init(&device->vma_mutex, NULL) != 0) {
  1509. result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
  1510. goto fail_fd;
  1511. }
  1512. /* keep the page with address zero out of the allocator */
  1513. util_vma_heap_init(&device->vma_lo, LOW_HEAP_MIN_ADDRESS, LOW_HEAP_SIZE);
  1514. device->vma_lo_available =
  1515. physical_device->memory.heaps[physical_device->memory.heap_count - 1].size;
  1516. /* Leave the last 4GiB out of the high vma range, so that no state base
  1517. * address + size can overflow 48 bits. For more information see the
  1518. * comment about Wa32bitGeneralStateOffset in anv_allocator.c
  1519. */
  1520. util_vma_heap_init(&device->vma_hi, HIGH_HEAP_MIN_ADDRESS,
  1521. HIGH_HEAP_SIZE);
  1522. device->vma_hi_available = physical_device->memory.heap_count == 1 ? 0 :
  1523. physical_device->memory.heaps[0].size;
  1524. }
  1525. /* As per spec, the driver implementation may deny requests to acquire
  1526. * a priority above the default priority (MEDIUM) if the caller does not
  1527. * have sufficient privileges. In this scenario VK_ERROR_NOT_PERMITTED_EXT
  1528. * is returned.
  1529. */
  1530. if (physical_device->has_context_priority) {
  1531. int err = anv_gem_set_context_param(device->fd, device->context_id,
  1532. I915_CONTEXT_PARAM_PRIORITY,
  1533. vk_priority_to_gen(priority));
  1534. if (err != 0 && priority > VK_QUEUE_GLOBAL_PRIORITY_MEDIUM_EXT) {
  1535. result = vk_error(VK_ERROR_NOT_PERMITTED_EXT);
  1536. goto fail_fd;
  1537. }
  1538. }
  1539. device->info = physical_device->info;
  1540. device->isl_dev = physical_device->isl_dev;
  1541. /* On Broadwell and later, we can use batch chaining to more efficiently
  1542. * implement growing command buffers. Prior to Haswell, the kernel
  1543. * command parser gets in the way and we have to fall back to growing
  1544. * the batch.
  1545. */
  1546. device->can_chain_batches = device->info.gen >= 8;
  1547. device->robust_buffer_access = pCreateInfo->pEnabledFeatures &&
  1548. pCreateInfo->pEnabledFeatures->robustBufferAccess;
  1549. device->enabled_extensions = enabled_extensions;
  1550. anv_device_init_dispatch(device);
  1551. if (pthread_mutex_init(&device->mutex, NULL) != 0) {
  1552. result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
  1553. goto fail_context_id;
  1554. }
  1555. pthread_condattr_t condattr;
  1556. if (pthread_condattr_init(&condattr) != 0) {
  1557. result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
  1558. goto fail_mutex;
  1559. }
  1560. if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC) != 0) {
  1561. pthread_condattr_destroy(&condattr);
  1562. result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
  1563. goto fail_mutex;
  1564. }
  1565. if (pthread_cond_init(&device->queue_submit, NULL) != 0) {
  1566. pthread_condattr_destroy(&condattr);
  1567. result = vk_error(VK_ERROR_INITIALIZATION_FAILED);
  1568. goto fail_mutex;
  1569. }
  1570. pthread_condattr_destroy(&condattr);
  1571. uint64_t bo_flags =
  1572. (physical_device->supports_48bit_addresses ? EXEC_OBJECT_SUPPORTS_48B_ADDRESS : 0) |
  1573. (physical_device->has_exec_async ? EXEC_OBJECT_ASYNC : 0) |
  1574. (physical_device->has_exec_capture ? EXEC_OBJECT_CAPTURE : 0) |
  1575. (physical_device->use_softpin ? EXEC_OBJECT_PINNED : 0);
  1576. anv_bo_pool_init(&device->batch_bo_pool, device, bo_flags);
  1577. result = anv_bo_cache_init(&device->bo_cache);
  1578. if (result != VK_SUCCESS)
  1579. goto fail_batch_bo_pool;
  1580. if (!physical_device->use_softpin)
  1581. bo_flags &= ~EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
  1582. result = anv_state_pool_init(&device->dynamic_state_pool, device,
  1583. DYNAMIC_STATE_POOL_MIN_ADDRESS,
  1584. 16384,
  1585. bo_flags);
  1586. if (result != VK_SUCCESS)
  1587. goto fail_bo_cache;
  1588. result = anv_state_pool_init(&device->instruction_state_pool, device,
  1589. INSTRUCTION_STATE_POOL_MIN_ADDRESS,
  1590. 16384,
  1591. bo_flags);
  1592. if (result != VK_SUCCESS)
  1593. goto fail_dynamic_state_pool;
  1594. result = anv_state_pool_init(&device->surface_state_pool, device,
  1595. SURFACE_STATE_POOL_MIN_ADDRESS,
  1596. 4096,
  1597. bo_flags);
  1598. if (result != VK_SUCCESS)
  1599. goto fail_instruction_state_pool;
  1600. if (physical_device->use_softpin) {
  1601. result = anv_state_pool_init(&device->binding_table_pool, device,
  1602. BINDING_TABLE_POOL_MIN_ADDRESS,
  1603. 4096,
  1604. bo_flags);
  1605. if (result != VK_SUCCESS)
  1606. goto fail_surface_state_pool;
  1607. }
  1608. result = anv_bo_init_new(&device->workaround_bo, device, 1024);
  1609. if (result != VK_SUCCESS)
  1610. goto fail_binding_table_pool;
  1611. if (physical_device->use_softpin)
  1612. device->workaround_bo.flags |= EXEC_OBJECT_PINNED;
  1613. if (!anv_vma_alloc(device, &device->workaround_bo))
  1614. goto fail_workaround_bo;
  1615. anv_device_init_trivial_batch(device);
  1616. if (device->info.gen >= 10)
  1617. anv_device_init_hiz_clear_value_bo(device);
  1618. anv_scratch_pool_init(device, &device->scratch_pool);
  1619. anv_queue_init(device, &device->queue);
  1620. switch (device->info.gen) {
  1621. case 7:
  1622. if (!device->info.is_haswell)
  1623. result = gen7_init_device_state(device);
  1624. else
  1625. result = gen75_init_device_state(device);
  1626. break;
  1627. case 8:
  1628. result = gen8_init_device_state(device);
  1629. break;
  1630. case 9:
  1631. result = gen9_init_device_state(device);
  1632. break;
  1633. case 10:
  1634. result = gen10_init_device_state(device);
  1635. break;
  1636. case 11:
  1637. result = gen11_init_device_state(device);
  1638. break;
  1639. default:
  1640. /* Shouldn't get here as we don't create physical devices for any other
  1641. * gens. */
  1642. unreachable("unhandled gen");
  1643. }
  1644. if (result != VK_SUCCESS)
  1645. goto fail_workaround_bo;
  1646. anv_pipeline_cache_init(&device->default_pipeline_cache, device, true);
  1647. anv_device_init_blorp(device);
  1648. anv_device_init_border_colors(device);
  1649. *pDevice = anv_device_to_handle(device);
  1650. return VK_SUCCESS;
  1651. fail_workaround_bo:
  1652. anv_queue_finish(&device->queue);
  1653. anv_scratch_pool_finish(device, &device->scratch_pool);
  1654. anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
  1655. anv_gem_close(device, device->workaround_bo.gem_handle);
  1656. fail_binding_table_pool:
  1657. if (physical_device->use_softpin)
  1658. anv_state_pool_finish(&device->binding_table_pool);
  1659. fail_surface_state_pool:
  1660. anv_state_pool_finish(&device->surface_state_pool);
  1661. fail_instruction_state_pool:
  1662. anv_state_pool_finish(&device->instruction_state_pool);
  1663. fail_dynamic_state_pool:
  1664. anv_state_pool_finish(&device->dynamic_state_pool);
  1665. fail_bo_cache:
  1666. anv_bo_cache_finish(&device->bo_cache);
  1667. fail_batch_bo_pool:
  1668. anv_bo_pool_finish(&device->batch_bo_pool);
  1669. pthread_cond_destroy(&device->queue_submit);
  1670. fail_mutex:
  1671. pthread_mutex_destroy(&device->mutex);
  1672. fail_context_id:
  1673. anv_gem_destroy_context(device, device->context_id);
  1674. fail_fd:
  1675. close(device->fd);
  1676. fail_device:
  1677. vk_free(&device->alloc, device);
  1678. return result;
  1679. }
  1680. void anv_DestroyDevice(
  1681. VkDevice _device,
  1682. const VkAllocationCallbacks* pAllocator)
  1683. {
  1684. ANV_FROM_HANDLE(anv_device, device, _device);
  1685. struct anv_physical_device *physical_device;
  1686. if (!device)
  1687. return;
  1688. physical_device = &device->instance->physicalDevice;
  1689. anv_device_finish_blorp(device);
  1690. anv_pipeline_cache_finish(&device->default_pipeline_cache);
  1691. anv_queue_finish(&device->queue);
  1692. #ifdef HAVE_VALGRIND
  1693. /* We only need to free these to prevent valgrind errors. The backing
  1694. * BO will go away in a couple of lines so we don't actually leak.
  1695. */
  1696. anv_state_pool_free(&device->dynamic_state_pool, device->border_colors);
  1697. #endif
  1698. anv_scratch_pool_finish(device, &device->scratch_pool);
  1699. anv_gem_munmap(device->workaround_bo.map, device->workaround_bo.size);
  1700. anv_vma_free(device, &device->workaround_bo);
  1701. anv_gem_close(device, device->workaround_bo.gem_handle);
  1702. anv_vma_free(device, &device->trivial_batch_bo);
  1703. anv_gem_close(device, device->trivial_batch_bo.gem_handle);
  1704. if (device->info.gen >= 10)
  1705. anv_gem_close(device, device->hiz_clear_bo.gem_handle);
  1706. if (physical_device->use_softpin)
  1707. anv_state_pool_finish(&device->binding_table_pool);
  1708. anv_state_pool_finish(&device->surface_state_pool);
  1709. anv_state_pool_finish(&device->instruction_state_pool);
  1710. anv_state_pool_finish(&device->dynamic_state_pool);
  1711. anv_bo_cache_finish(&device->bo_cache);
  1712. anv_bo_pool_finish(&device->batch_bo_pool);
  1713. pthread_cond_destroy(&device->queue_submit);
  1714. pthread_mutex_destroy(&device->mutex);
  1715. anv_gem_destroy_context(device, device->context_id);
  1716. close(device->fd);
  1717. vk_free(&device->alloc, device);
  1718. }
  1719. VkResult anv_EnumerateInstanceLayerProperties(
  1720. uint32_t* pPropertyCount,
  1721. VkLayerProperties* pProperties)
  1722. {
  1723. if (pProperties == NULL) {
  1724. *pPropertyCount = 0;
  1725. return VK_SUCCESS;
  1726. }
  1727. /* None supported at this time */
  1728. return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
  1729. }
  1730. VkResult anv_EnumerateDeviceLayerProperties(
  1731. VkPhysicalDevice physicalDevice,
  1732. uint32_t* pPropertyCount,
  1733. VkLayerProperties* pProperties)
  1734. {
  1735. if (pProperties == NULL) {
  1736. *pPropertyCount = 0;
  1737. return VK_SUCCESS;
  1738. }
  1739. /* None supported at this time */
  1740. return vk_error(VK_ERROR_LAYER_NOT_PRESENT);
  1741. }
  1742. void anv_GetDeviceQueue(
  1743. VkDevice _device,
  1744. uint32_t queueNodeIndex,
  1745. uint32_t queueIndex,
  1746. VkQueue* pQueue)
  1747. {
  1748. ANV_FROM_HANDLE(anv_device, device, _device);
  1749. assert(queueIndex == 0);
  1750. *pQueue = anv_queue_to_handle(&device->queue);
  1751. }
  1752. void anv_GetDeviceQueue2(
  1753. VkDevice _device,
  1754. const VkDeviceQueueInfo2* pQueueInfo,
  1755. VkQueue* pQueue)
  1756. {
  1757. ANV_FROM_HANDLE(anv_device, device, _device);
  1758. assert(pQueueInfo->queueIndex == 0);
  1759. if (pQueueInfo->flags == device->queue.flags)
  1760. *pQueue = anv_queue_to_handle(&device->queue);
  1761. else
  1762. *pQueue = NULL;
  1763. }
  1764. VkResult
  1765. _anv_device_set_lost(struct anv_device *device,
  1766. const char *file, int line,
  1767. const char *msg, ...)
  1768. {
  1769. VkResult err;
  1770. va_list ap;
  1771. device->_lost = true;
  1772. va_start(ap, msg);
  1773. err = __vk_errorv(device->instance, device,
  1774. VK_DEBUG_REPORT_OBJECT_TYPE_DEVICE_EXT,
  1775. VK_ERROR_DEVICE_LOST, file, line, msg, ap);
  1776. va_end(ap);
  1777. if (env_var_as_boolean("ANV_ABORT_ON_DEVICE_LOSS", false))
  1778. abort();
  1779. return err;
  1780. }
  1781. VkResult
  1782. anv_device_query_status(struct anv_device *device)
  1783. {
  1784. /* This isn't likely as most of the callers of this function already check
  1785. * for it. However, it doesn't hurt to check and it potentially lets us
  1786. * avoid an ioctl.
  1787. */
  1788. if (anv_device_is_lost(device))
  1789. return VK_ERROR_DEVICE_LOST;
  1790. uint32_t active, pending;
  1791. int ret = anv_gem_gpu_get_reset_stats(device, &active, &pending);
  1792. if (ret == -1) {
  1793. /* We don't know the real error. */
  1794. return anv_device_set_lost(device, "get_reset_stats failed: %m");
  1795. }
  1796. if (active) {
  1797. return anv_device_set_lost(device, "GPU hung on one of our command buffers");
  1798. } else if (pending) {
  1799. return anv_device_set_lost(device, "GPU hung with commands in-flight");
  1800. }
  1801. return VK_SUCCESS;
  1802. }
  1803. VkResult
  1804. anv_device_bo_busy(struct anv_device *device, struct anv_bo *bo)
  1805. {
  1806. /* Note: This only returns whether or not the BO is in use by an i915 GPU.
  1807. * Other usages of the BO (such as on different hardware) will not be
  1808. * flagged as "busy" by this ioctl. Use with care.
  1809. */
  1810. int ret = anv_gem_busy(device, bo->gem_handle);
  1811. if (ret == 1) {
  1812. return VK_NOT_READY;
  1813. } else if (ret == -1) {
  1814. /* We don't know the real error. */
  1815. return anv_device_set_lost(device, "gem wait failed: %m");
  1816. }
  1817. /* Query for device status after the busy call. If the BO we're checking
  1818. * got caught in a GPU hang we don't want to return VK_SUCCESS to the
  1819. * client because it clearly doesn't have valid data. Yes, this most
  1820. * likely means an ioctl, but we just did an ioctl to query the busy status
  1821. * so it's no great loss.
  1822. */
  1823. return anv_device_query_status(device);
  1824. }
  1825. VkResult
  1826. anv_device_wait(struct anv_device *device, struct anv_bo *bo,
  1827. int64_t timeout)
  1828. {
  1829. int ret = anv_gem_wait(device, bo->gem_handle, &timeout);
  1830. if (ret == -1 && errno == ETIME) {
  1831. return VK_TIMEOUT;
  1832. } else if (ret == -1) {
  1833. /* We don't know the real error. */
  1834. return anv_device_set_lost(device, "gem wait failed: %m");
  1835. }
  1836. /* Query for device status after the wait. If the BO we're waiting on got
  1837. * caught in a GPU hang we don't want to return VK_SUCCESS to the client
  1838. * because it clearly doesn't have valid data. Yes, this most likely means
  1839. * an ioctl, but we just did an ioctl to wait so it's no great loss.
  1840. */
  1841. return anv_device_query_status(device);
  1842. }
  1843. VkResult anv_DeviceWaitIdle(
  1844. VkDevice _device)
  1845. {
  1846. ANV_FROM_HANDLE(anv_device, device, _device);
  1847. if (anv_device_is_lost(device))
  1848. return VK_ERROR_DEVICE_LOST;
  1849. struct anv_batch batch;
  1850. uint32_t cmds[8];
  1851. batch.start = batch.next = cmds;
  1852. batch.end = (void *) cmds + sizeof(cmds);
  1853. anv_batch_emit(&batch, GEN7_MI_BATCH_BUFFER_END, bbe);
  1854. anv_batch_emit(&batch, GEN7_MI_NOOP, noop);
  1855. return anv_device_submit_simple_batch(device, &batch);
  1856. }
  1857. bool
  1858. anv_vma_alloc(struct anv_device *device, struct anv_bo *bo)
  1859. {
  1860. if (!(bo->flags & EXEC_OBJECT_PINNED))
  1861. return true;
  1862. pthread_mutex_lock(&device->vma_mutex);
  1863. bo->offset = 0;
  1864. if (bo->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS &&
  1865. device->vma_hi_available >= bo->size) {
  1866. uint64_t addr = util_vma_heap_alloc(&device->vma_hi, bo->size, 4096);
  1867. if (addr) {
  1868. bo->offset = gen_canonical_address(addr);
  1869. assert(addr == gen_48b_address(bo->offset));
  1870. device->vma_hi_available -= bo->size;
  1871. }
  1872. }
  1873. if (bo->offset == 0 && device->vma_lo_available >= bo->size) {
  1874. uint64_t addr = util_vma_heap_alloc(&device->vma_lo, bo->size, 4096);
  1875. if (addr) {
  1876. bo->offset = gen_canonical_address(addr);
  1877. assert(addr == gen_48b_address(bo->offset));
  1878. device->vma_lo_available -= bo->size;
  1879. }
  1880. }
  1881. pthread_mutex_unlock(&device->vma_mutex);
  1882. return bo->offset != 0;
  1883. }
  1884. void
  1885. anv_vma_free(struct anv_device *device, struct anv_bo *bo)
  1886. {
  1887. if (!(bo->flags & EXEC_OBJECT_PINNED))
  1888. return;
  1889. const uint64_t addr_48b = gen_48b_address(bo->offset);
  1890. pthread_mutex_lock(&device->vma_mutex);
  1891. if (addr_48b >= LOW_HEAP_MIN_ADDRESS &&
  1892. addr_48b <= LOW_HEAP_MAX_ADDRESS) {
  1893. util_vma_heap_free(&device->vma_lo, addr_48b, bo->size);
  1894. device->vma_lo_available += bo->size;
  1895. } else {
  1896. assert(addr_48b >= HIGH_HEAP_MIN_ADDRESS &&
  1897. addr_48b <= HIGH_HEAP_MAX_ADDRESS);
  1898. util_vma_heap_free(&device->vma_hi, addr_48b, bo->size);
  1899. device->vma_hi_available += bo->size;
  1900. }
  1901. pthread_mutex_unlock(&device->vma_mutex);
  1902. bo->offset = 0;
  1903. }
  1904. VkResult
  1905. anv_bo_init_new(struct anv_bo *bo, struct anv_device *device, uint64_t size)
  1906. {
  1907. uint32_t gem_handle = anv_gem_create(device, size);
  1908. if (!gem_handle)
  1909. return vk_error(VK_ERROR_OUT_OF_DEVICE_MEMORY);
  1910. anv_bo_init(bo, gem_handle, size);
  1911. return VK_SUCCESS;
  1912. }
  1913. VkResult anv_AllocateMemory(
  1914. VkDevice _device,
  1915. const VkMemoryAllocateInfo* pAllocateInfo,
  1916. const VkAllocationCallbacks* pAllocator,
  1917. VkDeviceMemory* pMem)
  1918. {
  1919. ANV_FROM_HANDLE(anv_device, device, _device);
  1920. struct anv_physical_device *pdevice = &device->instance->physicalDevice;
  1921. struct anv_device_memory *mem;
  1922. VkResult result = VK_SUCCESS;
  1923. assert(pAllocateInfo->sType == VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
  1924. /* The Vulkan 1.0.33 spec says "allocationSize must be greater than 0". */
  1925. assert(pAllocateInfo->allocationSize > 0);
  1926. if (pAllocateInfo->allocationSize > MAX_MEMORY_ALLOCATION_SIZE)
  1927. return VK_ERROR_OUT_OF_DEVICE_MEMORY;
  1928. /* FINISHME: Fail if allocation request exceeds heap size. */
  1929. mem = vk_alloc2(&device->alloc, pAllocator, sizeof(*mem), 8,
  1930. VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  1931. if (mem == NULL)
  1932. return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
  1933. assert(pAllocateInfo->memoryTypeIndex < pdevice->memory.type_count);
  1934. mem->type = &pdevice->memory.types[pAllocateInfo->memoryTypeIndex];
  1935. mem->map = NULL;
  1936. mem->map_size = 0;
  1937. mem->ahw = NULL;
  1938. uint64_t bo_flags = 0;
  1939. assert(mem->type->heapIndex < pdevice->memory.heap_count);
  1940. if (pdevice->memory.heaps[mem->type->heapIndex].supports_48bit_addresses)
  1941. bo_flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
  1942. const struct wsi_memory_allocate_info *wsi_info =
  1943. vk_find_struct_const(pAllocateInfo->pNext, WSI_MEMORY_ALLOCATE_INFO_MESA);
  1944. if (wsi_info && wsi_info->implicit_sync) {
  1945. /* We need to set the WRITE flag on window system buffers so that GEM
  1946. * will know we're writing to them and synchronize uses on other rings
  1947. * (eg if the display server uses the blitter ring).
  1948. */
  1949. bo_flags |= EXEC_OBJECT_WRITE;
  1950. } else if (pdevice->has_exec_async) {
  1951. bo_flags |= EXEC_OBJECT_ASYNC;
  1952. }
  1953. if (pdevice->use_softpin)
  1954. bo_flags |= EXEC_OBJECT_PINNED;
  1955. const VkExportMemoryAllocateInfo *export_info =
  1956. vk_find_struct_const(pAllocateInfo->pNext, EXPORT_MEMORY_ALLOCATE_INFO);
  1957. /* Check if we need to support Android HW buffer export. If so,
  1958. * create AHardwareBuffer and import memory from it.
  1959. */
  1960. bool android_export = false;
  1961. if (export_info && export_info->handleTypes &
  1962. VK_EXTERNAL_MEMORY_HANDLE_TYPE_ANDROID_HARDWARE_BUFFER_BIT_ANDROID)
  1963. android_export = true;
  1964. /* Android memory import. */
  1965. const struct VkImportAndroidHardwareBufferInfoANDROID *ahw_import_info =
  1966. vk_find_struct_const(pAllocateInfo->pNext,
  1967. IMPORT_ANDROID_HARDWARE_BUFFER_INFO_ANDROID);
  1968. if (ahw_import_info) {
  1969. result = anv_import_ahw_memory(_device, mem, ahw_import_info);
  1970. if (result != VK_SUCCESS)
  1971. goto fail;
  1972. goto success;
  1973. } else if (android_export) {
  1974. result = anv_create_ahw_memory(_device, mem, pAllocateInfo);
  1975. if (result != VK_SUCCESS)
  1976. goto fail;
  1977. const struct VkImportAndroidHardwareBufferInfoANDROID import_info = {
  1978. .buffer = mem->ahw,
  1979. };
  1980. result = anv_import_ahw_memory(_device, mem, &import_info);
  1981. if (result != VK_SUCCESS)
  1982. goto fail;
  1983. goto success;
  1984. }
  1985. const VkImportMemoryFdInfoKHR *fd_info =
  1986. vk_find_struct_const(pAllocateInfo->pNext, IMPORT_MEMORY_FD_INFO_KHR);
  1987. /* The Vulkan spec permits handleType to be 0, in which case the struct is
  1988. * ignored.
  1989. */
  1990. if (fd_info && fd_info->handleType) {
  1991. /* At the moment, we support only the below handle types. */
  1992. assert(fd_info->handleType ==
  1993. VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
  1994. fd_info->handleType ==
  1995. VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
  1996. result = anv_bo_cache_import(device, &device->bo_cache, fd_info->fd,
  1997. bo_flags | ANV_BO_EXTERNAL, &mem->bo);
  1998. if (result != VK_SUCCESS)
  1999. goto fail;
  2000. VkDeviceSize aligned_alloc_size =
  2001. align_u64(pAllocateInfo->allocationSize, 4096);
  2002. /* For security purposes, we reject importing the bo if it's smaller
  2003. * than the requested allocation size. This prevents a malicious client
  2004. * from passing a buffer to a trusted client, lying about the size, and
  2005. * telling the trusted client to try and texture from an image that goes
  2006. * out-of-bounds. This sort of thing could lead to GPU hangs or worse
  2007. * in the trusted client. The trusted client can protect itself against
  2008. * this sort of attack but only if it can trust the buffer size.
  2009. */
  2010. if (mem->bo->size < aligned_alloc_size) {
  2011. result = vk_errorf(device->instance, device,
  2012. VK_ERROR_INVALID_EXTERNAL_HANDLE,
  2013. "aligned allocationSize too large for "
  2014. "VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT: "
  2015. "%"PRIu64"B > %"PRIu64"B",
  2016. aligned_alloc_size, mem->bo->size);
  2017. anv_bo_cache_release(device, &device->bo_cache, mem->bo);
  2018. goto fail;
  2019. }
  2020. /* From the Vulkan spec:
  2021. *
  2022. * "Importing memory from a file descriptor transfers ownership of
  2023. * the file descriptor from the application to the Vulkan
  2024. * implementation. The application must not perform any operations on
  2025. * the file descriptor after a successful import."
  2026. *
  2027. * If the import fails, we leave the file descriptor open.
  2028. */
  2029. close(fd_info->fd);
  2030. goto success;
  2031. }
  2032. /* Regular allocate (not importing memory). */
  2033. if (export_info && export_info->handleTypes)
  2034. bo_flags |= ANV_BO_EXTERNAL;
  2035. result = anv_bo_cache_alloc(device, &device->bo_cache,
  2036. pAllocateInfo->allocationSize, bo_flags,
  2037. &mem->bo);
  2038. if (result != VK_SUCCESS)
  2039. goto fail;
  2040. const VkMemoryDedicatedAllocateInfo *dedicated_info =
  2041. vk_find_struct_const(pAllocateInfo->pNext, MEMORY_DEDICATED_ALLOCATE_INFO);
  2042. if (dedicated_info && dedicated_info->image != VK_NULL_HANDLE) {
  2043. ANV_FROM_HANDLE(anv_image, image, dedicated_info->image);
  2044. /* Some legacy (non-modifiers) consumers need the tiling to be set on
  2045. * the BO. In this case, we have a dedicated allocation.
  2046. */
  2047. if (image->needs_set_tiling) {
  2048. const uint32_t i915_tiling =
  2049. isl_tiling_to_i915_tiling(image->planes[0].surface.isl.tiling);
  2050. int ret = anv_gem_set_tiling(device, mem->bo->gem_handle,
  2051. image->planes[0].surface.isl.row_pitch_B,
  2052. i915_tiling);
  2053. if (ret) {
  2054. anv_bo_cache_release(device, &device->bo_cache, mem->bo);
  2055. return vk_errorf(device->instance, NULL,
  2056. VK_ERROR_OUT_OF_DEVICE_MEMORY,
  2057. "failed to set BO tiling: %m");
  2058. }
  2059. }
  2060. }
  2061. success:
  2062. *pMem = anv_device_memory_to_handle(mem);
  2063. return VK_SUCCESS;
  2064. fail:
  2065. vk_free2(&device->alloc, pAllocator, mem);
  2066. return result;
  2067. }
  2068. VkResult anv_GetMemoryFdKHR(
  2069. VkDevice device_h,
  2070. const VkMemoryGetFdInfoKHR* pGetFdInfo,
  2071. int* pFd)
  2072. {
  2073. ANV_FROM_HANDLE(anv_device, dev, device_h);
  2074. ANV_FROM_HANDLE(anv_device_memory, mem, pGetFdInfo->memory);
  2075. assert(pGetFdInfo->sType == VK_STRUCTURE_TYPE_MEMORY_GET_FD_INFO_KHR);
  2076. assert(pGetFdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_OPAQUE_FD_BIT ||
  2077. pGetFdInfo->handleType == VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT);
  2078. return anv_bo_cache_export(dev, &dev->bo_cache, mem->bo, pFd);
  2079. }
  2080. VkResult anv_GetMemoryFdPropertiesKHR(
  2081. VkDevice _device,
  2082. VkExternalMemoryHandleTypeFlagBits handleType,
  2083. int fd,
  2084. VkMemoryFdPropertiesKHR* pMemoryFdProperties)
  2085. {
  2086. ANV_FROM_HANDLE(anv_device, device, _device);
  2087. struct anv_physical_device *pdevice = &device->instance->physicalDevice;
  2088. switch (handleType) {
  2089. case VK_EXTERNAL_MEMORY_HANDLE_TYPE_DMA_BUF_BIT_EXT:
  2090. /* dma-buf can be imported as any memory type */
  2091. pMemoryFdProperties->memoryTypeBits =
  2092. (1 << pdevice->memory.type_count) - 1;
  2093. return VK_SUCCESS;
  2094. default:
  2095. /* The valid usage section for this function says:
  2096. *
  2097. * "handleType must not be one of the handle types defined as
  2098. * opaque."
  2099. *
  2100. * So opaque handle types fall into the default "unsupported" case.
  2101. */
  2102. return vk_error(VK_ERROR_INVALID_EXTERNAL_HANDLE);
  2103. }
  2104. }
  2105. void anv_FreeMemory(
  2106. VkDevice _device,
  2107. VkDeviceMemory _mem,
  2108. const VkAllocationCallbacks* pAllocator)
  2109. {
  2110. ANV_FROM_HANDLE(anv_device, device, _device);
  2111. ANV_FROM_HANDLE(anv_device_memory, mem, _mem);
  2112. if (mem == NULL)
  2113. return;
  2114. if (mem->map)
  2115. anv_UnmapMemory(_device, _mem);
  2116. anv_bo_cache_release(device, &device->bo_cache, mem->bo);
  2117. #ifdef ANDROID
  2118. if (mem->ahw)
  2119. AHardwareBuffer_release(mem->ahw);
  2120. #endif
  2121. vk_free2(&device->alloc, pAllocator, mem);
  2122. }
  2123. VkResult anv_MapMemory(
  2124. VkDevice _device,
  2125. VkDeviceMemory _memory,
  2126. VkDeviceSize offset,
  2127. VkDeviceSize size,
  2128. VkMemoryMapFlags flags,
  2129. void** ppData)
  2130. {
  2131. ANV_FROM_HANDLE(anv_device, device, _device);
  2132. ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
  2133. if (mem == NULL) {
  2134. *ppData = NULL;
  2135. return VK_SUCCESS;
  2136. }
  2137. if (size == VK_WHOLE_SIZE)
  2138. size = mem->bo->size - offset;
  2139. /* From the Vulkan spec version 1.0.32 docs for MapMemory:
  2140. *
  2141. * * If size is not equal to VK_WHOLE_SIZE, size must be greater than 0
  2142. * assert(size != 0);
  2143. * * If size is not equal to VK_WHOLE_SIZE, size must be less than or
  2144. * equal to the size of the memory minus offset
  2145. */
  2146. assert(size > 0);
  2147. assert(offset + size <= mem->bo->size);
  2148. /* FIXME: Is this supposed to be thread safe? Since vkUnmapMemory() only
  2149. * takes a VkDeviceMemory pointer, it seems like only one map of the memory
  2150. * at a time is valid. We could just mmap up front and return an offset
  2151. * pointer here, but that may exhaust virtual memory on 32 bit
  2152. * userspace. */
  2153. uint32_t gem_flags = 0;
  2154. if (!device->info.has_llc &&
  2155. (mem->type->propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT))
  2156. gem_flags |= I915_MMAP_WC;
  2157. /* GEM will fail to map if the offset isn't 4k-aligned. Round down. */
  2158. uint64_t map_offset = offset & ~4095ull;
  2159. assert(offset >= map_offset);
  2160. uint64_t map_size = (offset + size) - map_offset;
  2161. /* Let's map whole pages */
  2162. map_size = align_u64(map_size, 4096);
  2163. void *map = anv_gem_mmap(device, mem->bo->gem_handle,
  2164. map_offset, map_size, gem_flags);
  2165. if (map == MAP_FAILED)
  2166. return vk_error(VK_ERROR_MEMORY_MAP_FAILED);
  2167. mem->map = map;
  2168. mem->map_size = map_size;
  2169. *ppData = mem->map + (offset - map_offset);
  2170. return VK_SUCCESS;
  2171. }
  2172. void anv_UnmapMemory(
  2173. VkDevice _device,
  2174. VkDeviceMemory _memory)
  2175. {
  2176. ANV_FROM_HANDLE(anv_device_memory, mem, _memory);
  2177. if (mem == NULL)
  2178. return;
  2179. anv_gem_munmap(mem->map, mem->map_size);
  2180. mem->map = NULL;
  2181. mem->map_size = 0;
  2182. }
  2183. static void
  2184. clflush_mapped_ranges(struct anv_device *device,
  2185. uint32_t count,
  2186. const VkMappedMemoryRange *ranges)
  2187. {
  2188. for (uint32_t i = 0; i < count; i++) {
  2189. ANV_FROM_HANDLE(anv_device_memory, mem, ranges[i].memory);
  2190. if (ranges[i].offset >= mem->map_size)
  2191. continue;
  2192. gen_clflush_range(mem->map + ranges[i].offset,
  2193. MIN2(ranges[i].size, mem->map_size - ranges[i].offset));
  2194. }
  2195. }
  2196. VkResult anv_FlushMappedMemoryRanges(
  2197. VkDevice _device,
  2198. uint32_t memoryRangeCount,
  2199. const VkMappedMemoryRange* pMemoryRanges)
  2200. {
  2201. ANV_FROM_HANDLE(anv_device, device, _device);
  2202. if (device->info.has_llc)
  2203. return VK_SUCCESS;
  2204. /* Make sure the writes we're flushing have landed. */
  2205. __builtin_ia32_mfence();
  2206. clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
  2207. return VK_SUCCESS;
  2208. }
  2209. VkResult anv_InvalidateMappedMemoryRanges(
  2210. VkDevice _device,
  2211. uint32_t memoryRangeCount,
  2212. const VkMappedMemoryRange* pMemoryRanges)
  2213. {
  2214. ANV_FROM_HANDLE(anv_device, device, _device);
  2215. if (device->info.has_llc)
  2216. return VK_SUCCESS;
  2217. clflush_mapped_ranges(device, memoryRangeCount, pMemoryRanges);
  2218. /* Make sure no reads get moved up above the invalidate. */
  2219. __builtin_ia32_mfence();
  2220. return VK_SUCCESS;
  2221. }
  2222. void anv_GetBufferMemoryRequirements(
  2223. VkDevice _device,
  2224. VkBuffer _buffer,
  2225. VkMemoryRequirements* pMemoryRequirements)
  2226. {
  2227. ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
  2228. ANV_FROM_HANDLE(anv_device, device, _device);
  2229. struct anv_physical_device *pdevice = &device->instance->physicalDevice;
  2230. /* The Vulkan spec (git aaed022) says:
  2231. *
  2232. * memoryTypeBits is a bitfield and contains one bit set for every
  2233. * supported memory type for the resource. The bit `1<<i` is set if and
  2234. * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
  2235. * structure for the physical device is supported.
  2236. */
  2237. uint32_t memory_types = 0;
  2238. for (uint32_t i = 0; i < pdevice->memory.type_count; i++) {
  2239. uint32_t valid_usage = pdevice->memory.types[i].valid_buffer_usage;
  2240. if ((valid_usage & buffer->usage) == buffer->usage)
  2241. memory_types |= (1u << i);
  2242. }
  2243. /* Base alignment requirement of a cache line */
  2244. uint32_t alignment = 16;
  2245. /* We need an alignment of 32 for pushing UBOs */
  2246. if (buffer->usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT)
  2247. alignment = MAX2(alignment, 32);
  2248. pMemoryRequirements->size = buffer->size;
  2249. pMemoryRequirements->alignment = alignment;
  2250. /* Storage and Uniform buffers should have their size aligned to
  2251. * 32-bits to avoid boundary checks when last DWord is not complete.
  2252. * This would ensure that not internal padding would be needed for
  2253. * 16-bit types.
  2254. */
  2255. if (device->robust_buffer_access &&
  2256. (buffer->usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT ||
  2257. buffer->usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT))
  2258. pMemoryRequirements->size = align_u64(buffer->size, 4);
  2259. pMemoryRequirements->memoryTypeBits = memory_types;
  2260. }
  2261. void anv_GetBufferMemoryRequirements2(
  2262. VkDevice _device,
  2263. const VkBufferMemoryRequirementsInfo2* pInfo,
  2264. VkMemoryRequirements2* pMemoryRequirements)
  2265. {
  2266. anv_GetBufferMemoryRequirements(_device, pInfo->buffer,
  2267. &pMemoryRequirements->memoryRequirements);
  2268. vk_foreach_struct(ext, pMemoryRequirements->pNext) {
  2269. switch (ext->sType) {
  2270. case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
  2271. VkMemoryDedicatedRequirements *requirements = (void *)ext;
  2272. requirements->prefersDedicatedAllocation = VK_FALSE;
  2273. requirements->requiresDedicatedAllocation = VK_FALSE;
  2274. break;
  2275. }
  2276. default:
  2277. anv_debug_ignored_stype(ext->sType);
  2278. break;
  2279. }
  2280. }
  2281. }
  2282. void anv_GetImageMemoryRequirements(
  2283. VkDevice _device,
  2284. VkImage _image,
  2285. VkMemoryRequirements* pMemoryRequirements)
  2286. {
  2287. ANV_FROM_HANDLE(anv_image, image, _image);
  2288. ANV_FROM_HANDLE(anv_device, device, _device);
  2289. struct anv_physical_device *pdevice = &device->instance->physicalDevice;
  2290. /* The Vulkan spec (git aaed022) says:
  2291. *
  2292. * memoryTypeBits is a bitfield and contains one bit set for every
  2293. * supported memory type for the resource. The bit `1<<i` is set if and
  2294. * only if the memory type `i` in the VkPhysicalDeviceMemoryProperties
  2295. * structure for the physical device is supported.
  2296. *
  2297. * All types are currently supported for images.
  2298. */
  2299. uint32_t memory_types = (1ull << pdevice->memory.type_count) - 1;
  2300. /* We must have image allocated or imported at this point. According to the
  2301. * specification, external images must have been bound to memory before
  2302. * calling GetImageMemoryRequirements.
  2303. */
  2304. assert(image->size > 0);
  2305. pMemoryRequirements->size = image->size;
  2306. pMemoryRequirements->alignment = image->alignment;
  2307. pMemoryRequirements->memoryTypeBits = memory_types;
  2308. }
  2309. void anv_GetImageMemoryRequirements2(
  2310. VkDevice _device,
  2311. const VkImageMemoryRequirementsInfo2* pInfo,
  2312. VkMemoryRequirements2* pMemoryRequirements)
  2313. {
  2314. ANV_FROM_HANDLE(anv_device, device, _device);
  2315. ANV_FROM_HANDLE(anv_image, image, pInfo->image);
  2316. anv_GetImageMemoryRequirements(_device, pInfo->image,
  2317. &pMemoryRequirements->memoryRequirements);
  2318. vk_foreach_struct_const(ext, pInfo->pNext) {
  2319. switch (ext->sType) {
  2320. case VK_STRUCTURE_TYPE_IMAGE_PLANE_MEMORY_REQUIREMENTS_INFO: {
  2321. struct anv_physical_device *pdevice = &device->instance->physicalDevice;
  2322. const VkImagePlaneMemoryRequirementsInfo *plane_reqs =
  2323. (const VkImagePlaneMemoryRequirementsInfo *) ext;
  2324. uint32_t plane = anv_image_aspect_to_plane(image->aspects,
  2325. plane_reqs->planeAspect);
  2326. assert(image->planes[plane].offset == 0);
  2327. /* The Vulkan spec (git aaed022) says:
  2328. *
  2329. * memoryTypeBits is a bitfield and contains one bit set for every
  2330. * supported memory type for the resource. The bit `1<<i` is set
  2331. * if and only if the memory type `i` in the
  2332. * VkPhysicalDeviceMemoryProperties structure for the physical
  2333. * device is supported.
  2334. *
  2335. * All types are currently supported for images.
  2336. */
  2337. pMemoryRequirements->memoryRequirements.memoryTypeBits =
  2338. (1ull << pdevice->memory.type_count) - 1;
  2339. /* We must have image allocated or imported at this point. According to the
  2340. * specification, external images must have been bound to memory before
  2341. * calling GetImageMemoryRequirements.
  2342. */
  2343. assert(image->planes[plane].size > 0);
  2344. pMemoryRequirements->memoryRequirements.size = image->planes[plane].size;
  2345. pMemoryRequirements->memoryRequirements.alignment =
  2346. image->planes[plane].alignment;
  2347. break;
  2348. }
  2349. default:
  2350. anv_debug_ignored_stype(ext->sType);
  2351. break;
  2352. }
  2353. }
  2354. vk_foreach_struct(ext, pMemoryRequirements->pNext) {
  2355. switch (ext->sType) {
  2356. case VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS: {
  2357. VkMemoryDedicatedRequirements *requirements = (void *)ext;
  2358. if (image->needs_set_tiling || image->external_format) {
  2359. /* If we need to set the tiling for external consumers, we need a
  2360. * dedicated allocation.
  2361. *
  2362. * See also anv_AllocateMemory.
  2363. */
  2364. requirements->prefersDedicatedAllocation = VK_TRUE;
  2365. requirements->requiresDedicatedAllocation = VK_TRUE;
  2366. } else {
  2367. requirements->prefersDedicatedAllocation = VK_FALSE;
  2368. requirements->requiresDedicatedAllocation = VK_FALSE;
  2369. }
  2370. break;
  2371. }
  2372. default:
  2373. anv_debug_ignored_stype(ext->sType);
  2374. break;
  2375. }
  2376. }
  2377. }
  2378. void anv_GetImageSparseMemoryRequirements(
  2379. VkDevice device,
  2380. VkImage image,
  2381. uint32_t* pSparseMemoryRequirementCount,
  2382. VkSparseImageMemoryRequirements* pSparseMemoryRequirements)
  2383. {
  2384. *pSparseMemoryRequirementCount = 0;
  2385. }
  2386. void anv_GetImageSparseMemoryRequirements2(
  2387. VkDevice device,
  2388. const VkImageSparseMemoryRequirementsInfo2* pInfo,
  2389. uint32_t* pSparseMemoryRequirementCount,
  2390. VkSparseImageMemoryRequirements2* pSparseMemoryRequirements)
  2391. {
  2392. *pSparseMemoryRequirementCount = 0;
  2393. }
  2394. void anv_GetDeviceMemoryCommitment(
  2395. VkDevice device,
  2396. VkDeviceMemory memory,
  2397. VkDeviceSize* pCommittedMemoryInBytes)
  2398. {
  2399. *pCommittedMemoryInBytes = 0;
  2400. }
  2401. static void
  2402. anv_bind_buffer_memory(const VkBindBufferMemoryInfo *pBindInfo)
  2403. {
  2404. ANV_FROM_HANDLE(anv_device_memory, mem, pBindInfo->memory);
  2405. ANV_FROM_HANDLE(anv_buffer, buffer, pBindInfo->buffer);
  2406. assert(pBindInfo->sType == VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO);
  2407. if (mem) {
  2408. assert((buffer->usage & mem->type->valid_buffer_usage) == buffer->usage);
  2409. buffer->address = (struct anv_address) {
  2410. .bo = mem->bo,
  2411. .offset = pBindInfo->memoryOffset,
  2412. };
  2413. } else {
  2414. buffer->address = ANV_NULL_ADDRESS;
  2415. }
  2416. }
  2417. VkResult anv_BindBufferMemory(
  2418. VkDevice device,
  2419. VkBuffer buffer,
  2420. VkDeviceMemory memory,
  2421. VkDeviceSize memoryOffset)
  2422. {
  2423. anv_bind_buffer_memory(
  2424. &(VkBindBufferMemoryInfo) {
  2425. .sType = VK_STRUCTURE_TYPE_BIND_BUFFER_MEMORY_INFO,
  2426. .buffer = buffer,
  2427. .memory = memory,
  2428. .memoryOffset = memoryOffset,
  2429. });
  2430. return VK_SUCCESS;
  2431. }
  2432. VkResult anv_BindBufferMemory2(
  2433. VkDevice device,
  2434. uint32_t bindInfoCount,
  2435. const VkBindBufferMemoryInfo* pBindInfos)
  2436. {
  2437. for (uint32_t i = 0; i < bindInfoCount; i++)
  2438. anv_bind_buffer_memory(&pBindInfos[i]);
  2439. return VK_SUCCESS;
  2440. }
  2441. VkResult anv_QueueBindSparse(
  2442. VkQueue _queue,
  2443. uint32_t bindInfoCount,
  2444. const VkBindSparseInfo* pBindInfo,
  2445. VkFence fence)
  2446. {
  2447. ANV_FROM_HANDLE(anv_queue, queue, _queue);
  2448. if (anv_device_is_lost(queue->device))
  2449. return VK_ERROR_DEVICE_LOST;
  2450. return vk_error(VK_ERROR_FEATURE_NOT_PRESENT);
  2451. }
  2452. // Event functions
  2453. VkResult anv_CreateEvent(
  2454. VkDevice _device,
  2455. const VkEventCreateInfo* pCreateInfo,
  2456. const VkAllocationCallbacks* pAllocator,
  2457. VkEvent* pEvent)
  2458. {
  2459. ANV_FROM_HANDLE(anv_device, device, _device);
  2460. struct anv_state state;
  2461. struct anv_event *event;
  2462. assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_EVENT_CREATE_INFO);
  2463. state = anv_state_pool_alloc(&device->dynamic_state_pool,
  2464. sizeof(*event), 8);
  2465. event = state.map;
  2466. event->state = state;
  2467. event->semaphore = VK_EVENT_RESET;
  2468. if (!device->info.has_llc) {
  2469. /* Make sure the writes we're flushing have landed. */
  2470. __builtin_ia32_mfence();
  2471. __builtin_ia32_clflush(event);
  2472. }
  2473. *pEvent = anv_event_to_handle(event);
  2474. return VK_SUCCESS;
  2475. }
  2476. void anv_DestroyEvent(
  2477. VkDevice _device,
  2478. VkEvent _event,
  2479. const VkAllocationCallbacks* pAllocator)
  2480. {
  2481. ANV_FROM_HANDLE(anv_device, device, _device);
  2482. ANV_FROM_HANDLE(anv_event, event, _event);
  2483. if (!event)
  2484. return;
  2485. anv_state_pool_free(&device->dynamic_state_pool, event->state);
  2486. }
  2487. VkResult anv_GetEventStatus(
  2488. VkDevice _device,
  2489. VkEvent _event)
  2490. {
  2491. ANV_FROM_HANDLE(anv_device, device, _device);
  2492. ANV_FROM_HANDLE(anv_event, event, _event);
  2493. if (anv_device_is_lost(device))
  2494. return VK_ERROR_DEVICE_LOST;
  2495. if (!device->info.has_llc) {
  2496. /* Invalidate read cache before reading event written by GPU. */
  2497. __builtin_ia32_clflush(event);
  2498. __builtin_ia32_mfence();
  2499. }
  2500. return event->semaphore;
  2501. }
  2502. VkResult anv_SetEvent(
  2503. VkDevice _device,
  2504. VkEvent _event)
  2505. {
  2506. ANV_FROM_HANDLE(anv_device, device, _device);
  2507. ANV_FROM_HANDLE(anv_event, event, _event);
  2508. event->semaphore = VK_EVENT_SET;
  2509. if (!device->info.has_llc) {
  2510. /* Make sure the writes we're flushing have landed. */
  2511. __builtin_ia32_mfence();
  2512. __builtin_ia32_clflush(event);
  2513. }
  2514. return VK_SUCCESS;
  2515. }
  2516. VkResult anv_ResetEvent(
  2517. VkDevice _device,
  2518. VkEvent _event)
  2519. {
  2520. ANV_FROM_HANDLE(anv_device, device, _device);
  2521. ANV_FROM_HANDLE(anv_event, event, _event);
  2522. event->semaphore = VK_EVENT_RESET;
  2523. if (!device->info.has_llc) {
  2524. /* Make sure the writes we're flushing have landed. */
  2525. __builtin_ia32_mfence();
  2526. __builtin_ia32_clflush(event);
  2527. }
  2528. return VK_SUCCESS;
  2529. }
  2530. // Buffer functions
  2531. VkResult anv_CreateBuffer(
  2532. VkDevice _device,
  2533. const VkBufferCreateInfo* pCreateInfo,
  2534. const VkAllocationCallbacks* pAllocator,
  2535. VkBuffer* pBuffer)
  2536. {
  2537. ANV_FROM_HANDLE(anv_device, device, _device);
  2538. struct anv_buffer *buffer;
  2539. assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO);
  2540. buffer = vk_alloc2(&device->alloc, pAllocator, sizeof(*buffer), 8,
  2541. VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  2542. if (buffer == NULL)
  2543. return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
  2544. buffer->size = pCreateInfo->size;
  2545. buffer->usage = pCreateInfo->usage;
  2546. buffer->address = ANV_NULL_ADDRESS;
  2547. *pBuffer = anv_buffer_to_handle(buffer);
  2548. return VK_SUCCESS;
  2549. }
  2550. void anv_DestroyBuffer(
  2551. VkDevice _device,
  2552. VkBuffer _buffer,
  2553. const VkAllocationCallbacks* pAllocator)
  2554. {
  2555. ANV_FROM_HANDLE(anv_device, device, _device);
  2556. ANV_FROM_HANDLE(anv_buffer, buffer, _buffer);
  2557. if (!buffer)
  2558. return;
  2559. vk_free2(&device->alloc, pAllocator, buffer);
  2560. }
  2561. void
  2562. anv_fill_buffer_surface_state(struct anv_device *device, struct anv_state state,
  2563. enum isl_format format,
  2564. struct anv_address address,
  2565. uint32_t range, uint32_t stride)
  2566. {
  2567. isl_buffer_fill_state(&device->isl_dev, state.map,
  2568. .address = anv_address_physical(address),
  2569. .mocs = device->default_mocs,
  2570. .size_B = range,
  2571. .format = format,
  2572. .stride_B = stride);
  2573. }
  2574. void anv_DestroySampler(
  2575. VkDevice _device,
  2576. VkSampler _sampler,
  2577. const VkAllocationCallbacks* pAllocator)
  2578. {
  2579. ANV_FROM_HANDLE(anv_device, device, _device);
  2580. ANV_FROM_HANDLE(anv_sampler, sampler, _sampler);
  2581. if (!sampler)
  2582. return;
  2583. vk_free2(&device->alloc, pAllocator, sampler);
  2584. }
  2585. VkResult anv_CreateFramebuffer(
  2586. VkDevice _device,
  2587. const VkFramebufferCreateInfo* pCreateInfo,
  2588. const VkAllocationCallbacks* pAllocator,
  2589. VkFramebuffer* pFramebuffer)
  2590. {
  2591. ANV_FROM_HANDLE(anv_device, device, _device);
  2592. struct anv_framebuffer *framebuffer;
  2593. assert(pCreateInfo->sType == VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO);
  2594. size_t size = sizeof(*framebuffer) +
  2595. sizeof(struct anv_image_view *) * pCreateInfo->attachmentCount;
  2596. framebuffer = vk_alloc2(&device->alloc, pAllocator, size, 8,
  2597. VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
  2598. if (framebuffer == NULL)
  2599. return vk_error(VK_ERROR_OUT_OF_HOST_MEMORY);
  2600. framebuffer->attachment_count = pCreateInfo->attachmentCount;
  2601. for (uint32_t i = 0; i < pCreateInfo->attachmentCount; i++) {
  2602. VkImageView _iview = pCreateInfo->pAttachments[i];
  2603. framebuffer->attachments[i] = anv_image_view_from_handle(_iview);
  2604. }
  2605. framebuffer->width = pCreateInfo->width;
  2606. framebuffer->height = pCreateInfo->height;
  2607. framebuffer->layers = pCreateInfo->layers;
  2608. *pFramebuffer = anv_framebuffer_to_handle(framebuffer);
  2609. return VK_SUCCESS;
  2610. }
  2611. void anv_DestroyFramebuffer(
  2612. VkDevice _device,
  2613. VkFramebuffer _fb,
  2614. const VkAllocationCallbacks* pAllocator)
  2615. {
  2616. ANV_FROM_HANDLE(anv_device, device, _device);
  2617. ANV_FROM_HANDLE(anv_framebuffer, fb, _fb);
  2618. if (!fb)
  2619. return;
  2620. vk_free2(&device->alloc, pAllocator, fb);
  2621. }
  2622. static const VkTimeDomainEXT anv_time_domains[] = {
  2623. VK_TIME_DOMAIN_DEVICE_EXT,
  2624. VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT,
  2625. VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT,
  2626. };
  2627. VkResult anv_GetPhysicalDeviceCalibrateableTimeDomainsEXT(
  2628. VkPhysicalDevice physicalDevice,
  2629. uint32_t *pTimeDomainCount,
  2630. VkTimeDomainEXT *pTimeDomains)
  2631. {
  2632. int d;
  2633. VK_OUTARRAY_MAKE(out, pTimeDomains, pTimeDomainCount);
  2634. for (d = 0; d < ARRAY_SIZE(anv_time_domains); d++) {
  2635. vk_outarray_append(&out, i) {
  2636. *i = anv_time_domains[d];
  2637. }
  2638. }
  2639. return vk_outarray_status(&out);
  2640. }
  2641. static uint64_t
  2642. anv_clock_gettime(clockid_t clock_id)
  2643. {
  2644. struct timespec current;
  2645. int ret;
  2646. ret = clock_gettime(clock_id, &current);
  2647. if (ret < 0 && clock_id == CLOCK_MONOTONIC_RAW)
  2648. ret = clock_gettime(CLOCK_MONOTONIC, &current);
  2649. if (ret < 0)
  2650. return 0;
  2651. return (uint64_t) current.tv_sec * 1000000000ULL + current.tv_nsec;
  2652. }
  2653. #define TIMESTAMP 0x2358
  2654. VkResult anv_GetCalibratedTimestampsEXT(
  2655. VkDevice _device,
  2656. uint32_t timestampCount,
  2657. const VkCalibratedTimestampInfoEXT *pTimestampInfos,
  2658. uint64_t *pTimestamps,
  2659. uint64_t *pMaxDeviation)
  2660. {
  2661. ANV_FROM_HANDLE(anv_device, device, _device);
  2662. uint64_t timestamp_frequency = device->info.timestamp_frequency;
  2663. int ret;
  2664. int d;
  2665. uint64_t begin, end;
  2666. uint64_t max_clock_period = 0;
  2667. begin = anv_clock_gettime(CLOCK_MONOTONIC_RAW);
  2668. for (d = 0; d < timestampCount; d++) {
  2669. switch (pTimestampInfos[d].timeDomain) {
  2670. case VK_TIME_DOMAIN_DEVICE_EXT:
  2671. ret = anv_gem_reg_read(device, TIMESTAMP | 1,
  2672. &pTimestamps[d]);
  2673. if (ret != 0) {
  2674. return anv_device_set_lost(device, "Failed to read the TIMESTAMP "
  2675. "register: %m");
  2676. }
  2677. uint64_t device_period = DIV_ROUND_UP(1000000000, timestamp_frequency);
  2678. max_clock_period = MAX2(max_clock_period, device_period);
  2679. break;
  2680. case VK_TIME_DOMAIN_CLOCK_MONOTONIC_EXT:
  2681. pTimestamps[d] = anv_clock_gettime(CLOCK_MONOTONIC);
  2682. max_clock_period = MAX2(max_clock_period, 1);
  2683. break;
  2684. case VK_TIME_DOMAIN_CLOCK_MONOTONIC_RAW_EXT:
  2685. pTimestamps[d] = begin;
  2686. break;
  2687. default:
  2688. pTimestamps[d] = 0;
  2689. break;
  2690. }
  2691. }
  2692. end = anv_clock_gettime(CLOCK_MONOTONIC_RAW);
  2693. /*
  2694. * The maximum deviation is the sum of the interval over which we
  2695. * perform the sampling and the maximum period of any sampled
  2696. * clock. That's because the maximum skew between any two sampled
  2697. * clock edges is when the sampled clock with the largest period is
  2698. * sampled at the end of that period but right at the beginning of the
  2699. * sampling interval and some other clock is sampled right at the
  2700. * begining of its sampling period and right at the end of the
  2701. * sampling interval. Let's assume the GPU has the longest clock
  2702. * period and that the application is sampling GPU and monotonic:
  2703. *
  2704. * s e
  2705. * w x y z 0 1 2 3 4 5 6 7 8 9 a b c d e f
  2706. * Raw -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
  2707. *
  2708. * g
  2709. * 0 1 2 3
  2710. * GPU -----_____-----_____-----_____-----_____
  2711. *
  2712. * m
  2713. * x y z 0 1 2 3 4 5 6 7 8 9 a b c
  2714. * Monotonic -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
  2715. *
  2716. * Interval <----------------->
  2717. * Deviation <-------------------------->
  2718. *
  2719. * s = read(raw) 2
  2720. * g = read(GPU) 1
  2721. * m = read(monotonic) 2
  2722. * e = read(raw) b
  2723. *
  2724. * We round the sample interval up by one tick to cover sampling error
  2725. * in the interval clock
  2726. */
  2727. uint64_t sample_interval = end - begin + 1;
  2728. *pMaxDeviation = sample_interval + max_clock_period;
  2729. return VK_SUCCESS;
  2730. }
  2731. /* vk_icd.h does not declare this function, so we declare it here to
  2732. * suppress Wmissing-prototypes.
  2733. */
  2734. PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
  2735. vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion);
  2736. PUBLIC VKAPI_ATTR VkResult VKAPI_CALL
  2737. vk_icdNegotiateLoaderICDInterfaceVersion(uint32_t* pSupportedVersion)
  2738. {
  2739. /* For the full details on loader interface versioning, see
  2740. * <https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/blob/master/loader/LoaderAndLayerInterface.md>.
  2741. * What follows is a condensed summary, to help you navigate the large and
  2742. * confusing official doc.
  2743. *
  2744. * - Loader interface v0 is incompatible with later versions. We don't
  2745. * support it.
  2746. *
  2747. * - In loader interface v1:
  2748. * - The first ICD entrypoint called by the loader is
  2749. * vk_icdGetInstanceProcAddr(). The ICD must statically expose this
  2750. * entrypoint.
  2751. * - The ICD must statically expose no other Vulkan symbol unless it is
  2752. * linked with -Bsymbolic.
  2753. * - Each dispatchable Vulkan handle created by the ICD must be
  2754. * a pointer to a struct whose first member is VK_LOADER_DATA. The
  2755. * ICD must initialize VK_LOADER_DATA.loadMagic to ICD_LOADER_MAGIC.
  2756. * - The loader implements vkCreate{PLATFORM}SurfaceKHR() and
  2757. * vkDestroySurfaceKHR(). The ICD must be capable of working with
  2758. * such loader-managed surfaces.
  2759. *
  2760. * - Loader interface v2 differs from v1 in:
  2761. * - The first ICD entrypoint called by the loader is
  2762. * vk_icdNegotiateLoaderICDInterfaceVersion(). The ICD must
  2763. * statically expose this entrypoint.
  2764. *
  2765. * - Loader interface v3 differs from v2 in:
  2766. * - The ICD must implement vkCreate{PLATFORM}SurfaceKHR(),
  2767. * vkDestroySurfaceKHR(), and other API which uses VKSurfaceKHR,
  2768. * because the loader no longer does so.
  2769. */
  2770. *pSupportedVersion = MIN2(*pSupportedVersion, 3u);
  2771. return VK_SUCCESS;
  2772. }