Home | History | Annotate | Line # | Download | only in i915
      1 /*	$NetBSD: intel_memory_region.h,v 1.2 2021/12/18 23:45:28 riastradh Exp $	*/
      2 
      3 /* SPDX-License-Identifier: MIT */
      4 /*
      5  * Copyright  2019 Intel Corporation
      6  */
      7 
      8 #ifndef __INTEL_MEMORY_REGION_H__
      9 #define __INTEL_MEMORY_REGION_H__
     10 
     11 #include <linux/kref.h>
     12 #include <linux/ioport.h>
     13 #include <linux/mutex.h>
     14 #include <linux/io-mapping.h>
     15 #include <drm/drm_mm.h>
     16 
     17 #include "i915_buddy.h"
     18 
     19 struct drm_i915_private;
     20 struct drm_i915_gem_object;
     21 struct intel_memory_region;
     22 struct sg_table;
     23 
     24 /**
     25  *  Base memory type
     26  */
     27 enum intel_memory_type {
     28 	INTEL_MEMORY_SYSTEM = 0,
     29 	INTEL_MEMORY_LOCAL,
     30 	INTEL_MEMORY_STOLEN,
     31 };
     32 
     33 enum intel_region_id {
     34 	INTEL_REGION_SMEM = 0,
     35 	INTEL_REGION_LMEM,
     36 	INTEL_REGION_STOLEN,
     37 	INTEL_REGION_UNKNOWN, /* Should be last */
     38 };
     39 
     40 #define REGION_SMEM     BIT(INTEL_REGION_SMEM)
     41 #define REGION_LMEM     BIT(INTEL_REGION_LMEM)
     42 #define REGION_STOLEN   BIT(INTEL_REGION_STOLEN)
     43 
     44 #define INTEL_MEMORY_TYPE_SHIFT 16
     45 
     46 #define MEMORY_TYPE_FROM_REGION(r) (ilog2((r) >> INTEL_MEMORY_TYPE_SHIFT))
     47 #define MEMORY_INSTANCE_FROM_REGION(r) (ilog2((r) & 0xffff))
     48 
     49 #define I915_ALLOC_MIN_PAGE_SIZE  BIT(0)
     50 #define I915_ALLOC_CONTIGUOUS     BIT(1)
     51 
     52 #define for_each_memory_region(mr, i915, id) \
     53 	for (id = 0; id < ARRAY_SIZE((i915)->mm.regions); id++) \
     54 		for_each_if((mr) = (i915)->mm.regions[id])
     55 
     56 /**
     57  * Memory regions encoded as type | instance
     58  */
     59 extern const u32 intel_region_map[];
     60 
     61 struct intel_memory_region_ops {
     62 	unsigned int flags;
     63 
     64 	int (*init)(struct intel_memory_region *mem);
     65 	void (*release)(struct intel_memory_region *mem);
     66 
     67 	struct drm_i915_gem_object *
     68 	(*create_object)(struct intel_memory_region *mem,
     69 			 resource_size_t size,
     70 			 unsigned int flags);
     71 };
     72 
     73 struct intel_memory_region {
     74 	struct drm_i915_private *i915;
     75 
     76 	const struct intel_memory_region_ops *ops;
     77 
     78 	struct io_mapping iomap;
     79 	struct resource region;
     80 
     81 	/* For fake LMEM */
     82 	struct drm_mm_node fake_mappable;
     83 
     84 	struct i915_buddy_mm mm;
     85 	struct mutex mm_lock;
     86 
     87 	struct kref kref;
     88 
     89 	resource_size_t io_start;
     90 	resource_size_t min_page_size;
     91 	resource_size_t total;
     92 	resource_size_t avail;
     93 
     94 	unsigned int type;
     95 	unsigned int instance;
     96 	unsigned int id;
     97 	char name[8];
     98 
     99 	dma_addr_t remap_addr;
    100 
    101 	struct {
    102 		struct mutex lock; /* Protects access to objects */
    103 		struct list_head list;
    104 		struct list_head purgeable;
    105 	} objects;
    106 };
    107 
    108 int intel_memory_region_init_buddy(struct intel_memory_region *mem);
    109 void intel_memory_region_release_buddy(struct intel_memory_region *mem);
    110 
    111 int __intel_memory_region_get_pages_buddy(struct intel_memory_region *mem,
    112 					  resource_size_t size,
    113 					  unsigned int flags,
    114 					  struct list_head *blocks);
    115 struct i915_buddy_block *
    116 __intel_memory_region_get_block_buddy(struct intel_memory_region *mem,
    117 				      resource_size_t size,
    118 				      unsigned int flags);
    119 void __intel_memory_region_put_pages_buddy(struct intel_memory_region *mem,
    120 					   struct list_head *blocks);
    121 void __intel_memory_region_put_block_buddy(struct i915_buddy_block *block);
    122 
    123 struct intel_memory_region *
    124 intel_memory_region_create(struct drm_i915_private *i915,
    125 			   resource_size_t start,
    126 			   resource_size_t size,
    127 			   resource_size_t min_page_size,
    128 			   resource_size_t io_start,
    129 			   const struct intel_memory_region_ops *ops);
    130 
    131 struct intel_memory_region *
    132 intel_memory_region_get(struct intel_memory_region *mem);
    133 void intel_memory_region_put(struct intel_memory_region *mem);
    134 
    135 int intel_memory_regions_hw_probe(struct drm_i915_private *i915);
    136 void intel_memory_regions_driver_release(struct drm_i915_private *i915);
    137 struct intel_memory_region *
    138 intel_memory_region_by_type(struct drm_i915_private *i915,
    139 			    enum intel_memory_type mem_type);
    140 
    141 __printf(2, 3) void
    142 intel_memory_region_set_name(struct intel_memory_region *mem,
    143 			     const char *fmt, ...);
    144 
    145 #endif
    146