0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025 #ifndef _TTM_RESOURCE_H_
0026 #define _TTM_RESOURCE_H_
0027
0028 #include <linux/types.h>
0029 #include <linux/list.h>
0030 #include <linux/mutex.h>
0031 #include <linux/iosys-map.h>
0032 #include <linux/dma-fence.h>
0033
0034 #include <drm/drm_print.h>
0035 #include <drm/ttm/ttm_caching.h>
0036 #include <drm/ttm/ttm_kmap_iter.h>
0037
0038 #define TTM_MAX_BO_PRIORITY 4U
0039 #define TTM_NUM_MEM_TYPES 8
0040
0041 struct ttm_device;
0042 struct ttm_resource_manager;
0043 struct ttm_resource;
0044 struct ttm_place;
0045 struct ttm_buffer_object;
0046 struct ttm_placement;
0047 struct iosys_map;
0048 struct io_mapping;
0049 struct sg_table;
0050 struct scatterlist;
0051
0052 struct ttm_resource_manager_func {
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074 int (*alloc)(struct ttm_resource_manager *man,
0075 struct ttm_buffer_object *bo,
0076 const struct ttm_place *place,
0077 struct ttm_resource **res);
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088 void (*free)(struct ttm_resource_manager *man,
0089 struct ttm_resource *res);
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101 void (*debug)(struct ttm_resource_manager *man,
0102 struct drm_printer *printer);
0103 };
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119 struct ttm_resource_manager {
0120
0121
0122
0123 bool use_type;
0124 bool use_tt;
0125 struct ttm_device *bdev;
0126 uint64_t size;
0127 const struct ttm_resource_manager_func *func;
0128 spinlock_t move_lock;
0129
0130
0131
0132
0133 struct dma_fence *move;
0134
0135
0136
0137
0138 struct list_head lru[TTM_MAX_BO_PRIORITY];
0139
0140
0141
0142
0143
0144 uint64_t usage;
0145 };
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157 struct ttm_bus_placement {
0158 void *addr;
0159 phys_addr_t offset;
0160 bool is_iomem;
0161 enum ttm_caching caching;
0162 };
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 struct ttm_resource {
0178 unsigned long start;
0179 unsigned long num_pages;
0180 uint32_t mem_type;
0181 uint32_t placement;
0182 struct ttm_bus_placement bus;
0183 struct ttm_buffer_object *bo;
0184
0185
0186
0187
0188 struct list_head lru;
0189 };
0190
0191
0192
0193
0194
0195
0196
0197
0198 struct ttm_resource_cursor {
0199 unsigned int priority;
0200 };
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210 struct ttm_lru_bulk_move_pos {
0211 struct ttm_resource *first;
0212 struct ttm_resource *last;
0213 };
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223 struct ttm_lru_bulk_move {
0224 struct ttm_lru_bulk_move_pos pos[TTM_NUM_MEM_TYPES][TTM_MAX_BO_PRIORITY];
0225 };
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241 struct ttm_kmap_iter_iomap {
0242 struct ttm_kmap_iter base;
0243 struct io_mapping *iomap;
0244 struct sg_table *st;
0245 resource_size_t start;
0246 struct {
0247 struct scatterlist *sg;
0248 pgoff_t i;
0249 pgoff_t end;
0250 pgoff_t offs;
0251 } cache;
0252 };
0253
0254
0255
0256
0257
0258
0259
0260 struct ttm_kmap_iter_linear_io {
0261 struct ttm_kmap_iter base;
0262 struct iosys_map dmap;
0263 bool needs_unmap;
0264 };
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275 static inline void
0276 ttm_resource_manager_set_used(struct ttm_resource_manager *man, bool used)
0277 {
0278 int i;
0279
0280 for (i = 0; i < TTM_MAX_BO_PRIORITY; i++)
0281 WARN_ON(!list_empty(&man->lru[i]));
0282 man->use_type = used;
0283 }
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294 static inline bool ttm_resource_manager_used(struct ttm_resource_manager *man)
0295 {
0296 return man->use_type;
0297 }
0298
0299
0300
0301
0302
0303
0304
0305
0306 static inline void
0307 ttm_resource_manager_cleanup(struct ttm_resource_manager *man)
0308 {
0309 dma_fence_put(man->move);
0310 man->move = NULL;
0311 }
0312
0313 void ttm_lru_bulk_move_init(struct ttm_lru_bulk_move *bulk);
0314 void ttm_lru_bulk_move_tail(struct ttm_lru_bulk_move *bulk);
0315
0316 void ttm_resource_add_bulk_move(struct ttm_resource *res,
0317 struct ttm_buffer_object *bo);
0318 void ttm_resource_del_bulk_move(struct ttm_resource *res,
0319 struct ttm_buffer_object *bo);
0320 void ttm_resource_move_to_lru_tail(struct ttm_resource *res);
0321
0322 void ttm_resource_init(struct ttm_buffer_object *bo,
0323 const struct ttm_place *place,
0324 struct ttm_resource *res);
0325 void ttm_resource_fini(struct ttm_resource_manager *man,
0326 struct ttm_resource *res);
0327
0328 int ttm_resource_alloc(struct ttm_buffer_object *bo,
0329 const struct ttm_place *place,
0330 struct ttm_resource **res);
0331 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res);
0332 bool ttm_resource_compat(struct ttm_resource *res,
0333 struct ttm_placement *placement);
0334 void ttm_resource_set_bo(struct ttm_resource *res,
0335 struct ttm_buffer_object *bo);
0336
0337 void ttm_resource_manager_init(struct ttm_resource_manager *man,
0338 struct ttm_device *bdev,
0339 uint64_t size);
0340
0341 int ttm_resource_manager_evict_all(struct ttm_device *bdev,
0342 struct ttm_resource_manager *man);
0343
0344 uint64_t ttm_resource_manager_usage(struct ttm_resource_manager *man);
0345 void ttm_resource_manager_debug(struct ttm_resource_manager *man,
0346 struct drm_printer *p);
0347
0348 struct ttm_resource *
0349 ttm_resource_manager_first(struct ttm_resource_manager *man,
0350 struct ttm_resource_cursor *cursor);
0351 struct ttm_resource *
0352 ttm_resource_manager_next(struct ttm_resource_manager *man,
0353 struct ttm_resource_cursor *cursor,
0354 struct ttm_resource *res);
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364 #define ttm_resource_manager_for_each_res(man, cursor, res) \
0365 for (res = ttm_resource_manager_first(man, cursor); res; \
0366 res = ttm_resource_manager_next(man, cursor, res))
0367
0368 struct ttm_kmap_iter *
0369 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
0370 struct io_mapping *iomap,
0371 struct sg_table *st,
0372 resource_size_t start);
0373
0374 struct ttm_kmap_iter_linear_io;
0375
0376 struct ttm_kmap_iter *
0377 ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io,
0378 struct ttm_device *bdev,
0379 struct ttm_resource *mem);
0380
0381 void ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io,
0382 struct ttm_device *bdev,
0383 struct ttm_resource *mem);
0384
0385 void ttm_resource_manager_create_debugfs(struct ttm_resource_manager *man,
0386 struct dentry * parent,
0387 const char *name);
0388 #endif