Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

anv_device.c 105KB

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