+ screen->has_svm = false;
+ /* we only care about HMM with OpenCL enabled */
+ if (dev->chipset > 0x130 && screen->force_enable_cl) {
+ /* Before being able to enable SVM we need to carve out some memory for
+ * driver bo allocations. Let's just base the size on the available VRAM.
+ *
+ * 40 bit is the biggest we care about and for 32 bit systems we don't
+ * want to allocate all of the available memory either.
+ *
+ * Also we align the size we want to reserve to the next POT to make use
+ * of hugepages.
+ */
+ const int vram_shift = util_logbase2_ceil64(dev->vram_size);
+ const int limit_bit =
+ MIN2(sizeof(void*) * 8 - 1, NV_GENERIC_VM_LIMIT_SHIFT);
+ screen->svm_cutout_size =
+ BITFIELD64_BIT(MIN2(sizeof(void*) == 4 ? 26 : NV_GENERIC_VM_LIMIT_SHIFT, vram_shift));
+
+ size_t start = screen->svm_cutout_size;
+ do {
+ screen->svm_cutout = reserve_vma(start, screen->svm_cutout_size);
+ if (!screen->svm_cutout) {
+ start += screen->svm_cutout_size;
+ continue;
+ }
+
+ struct drm_nouveau_svm_init svm_args = {
+ .unmanaged_addr = (uint64_t)screen->svm_cutout,
+ .unmanaged_size = screen->svm_cutout_size,
+ };
+
+ ret = drmCommandWrite(screen->drm->fd, DRM_NOUVEAU_SVM_INIT,
+ &svm_args, sizeof(svm_args));
+ screen->has_svm = !ret;
+ if (!screen->has_svm)
+ os_munmap(screen->svm_cutout, screen->svm_cutout_size);
+ break;
+ } while ((start + screen->svm_cutout_size) < BITFIELD64_MASK(limit_bit));
+ }
+