1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
|
/* Copyright (c) 2010, The Linux Foundation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 and
* only version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#ifndef _VCM_H_
#define _VCM_H_
/* All undefined types must be defined using platform specific headers */
#include <linux/vcm_types.h>
/*
* Virtual contiguous memory (VCM) region primitives.
*
* Current memory mapping software uses a CPU centric management
* model. This makes sense in general, average hardware only contains an
* CPU MMU and possibly a graphics MMU. If every device in the system
* has one or more MMUs a CPU centric MM programming model breaks down.
*
* Looking at mapping from a system-wide perspective reveals a general
* graph problem. Each node that talks to memory, either through an MMU
* or directly (via physical memory) can be thought of as the device end
* of a mapping edge. The other edge is the physical memory that is
* mapped.
*
* In the direct mapped case, it is useful to give the device an
* MMU. This one-to-one MMU allows direct mapped devices to
* participate in graph management, they simply see memory through a
* one-to-one mapping.
*
* The CPU nodes can also be brought under the same mapping
* abstraction with the use of a light overlay on the existing
* VMM. This light overlay brings the VMM's page table abstraction for
* each process and the kernel into the graph management API.
*
* Taken together this system wide approach provides a capability that
* is greater than the sum of its parts by allowing users to reason
* about system wide mapping issues without getting bogged down in CPU
* centric device page table management issues.
*/
/*
* Creating, freeing and managing VCMs.
*
* A VCM region is a virtual space that can be reserved from and
* associated with one or more devices. At creation the user can
* specify an offset to start addresses and a length of the entire VCM
* region. Reservations out of a VCM region are always contiguous.
*/
/**
* vcm_create() - Create a VCM region
* @start_addr: The starting address of the VCM region.
* @len: The len of the VCM region. This must be at least
* vcm_get_min_page_size() bytes.
*
* A VCM typically abstracts a page table.
*
* All functions in this API are passed and return opaque things
* because the underlying implementations will vary. The goal
* is really graph management. vcm_create() creates the "device end"
* of an edge in the mapping graph.
*
* The return value is non-zero if a VCM has successfully been
* created. It will return zero if a VCM region cannot be created or
* len is invalid.
*/
struct vcm *vcm_create(unsigned long start_addr, size_t len);
/**
* vcm_create_from_prebuilt() - Create a VCM region from an existing region
* @ext_vcm_id: An external opaque value that allows the
* implementation to reference an already built table.
*
* The ext_vcm_id will probably reference a page table that's been built
* by the VM.
*
* The platform specific implementation will provide this.
*
* The return value is non-zero if a VCM has successfully been created.
*/
struct vcm *vcm_create_from_prebuilt(size_t ext_vcm_id);
/**
* vcm_clone() - Clone a VCM
* @vcm: A VCM to clone from.
*
* Perform a VCM "deep copy." The resulting VCM will match the original at
* the point of cloning. Subsequent updates to either VCM will only be
* seen by that VCM.
*
* The return value is non-zero if a VCM has been successfully cloned.
*/
struct vcm *vcm_clone(struct vcm *vcm);
/**
* vcm_get_start_addr() - Get the starting address of the VCM region.
* @vcm: The VCM we're interested in getting the starting
* address of.
*
* The return value will be 1 if an error has occurred.
*/
size_t vcm_get_start_addr(struct vcm *vcm);
/**
* vcm_get_len() - Get the length of the VCM region.
* @vcm: The VCM we're interested in reading the length from.
*
* The return value will be non-zero for a valid VCM. VCM regions
* cannot have 0 len.
*/
size_t vcm_get_len(struct vcm *vcm);
/**
* vcm_free() - Free a VCM.
* @vcm: The VCM we're interested in freeing.
*
* The return value is 0 if the VCM has been freed or:
* -EBUSY The VCM region contains reservations or has been
* associated (active or not) and cannot be freed.
* -EINVAL The vcm argument is invalid.
*/
int vcm_free(struct vcm *vcm);
/*
* Creating, freeing and managing reservations out of a VCM.
*
*/
/**
* vcm_reserve() - Create a reservation from a VCM region.
* @vcm: The VCM region to reserve from.
* @len: The length of the reservation. Must be at least
* vcm_get_min_page_size() bytes.
* @attr: See 'Reservation Attributes'.
*
* A reservation, res_t, is a contiguous range from a VCM region.
*
* The return value is non-zero if a reservation has been successfully
* created. It is 0 if any of the parameters are invalid.
*/
struct res *vcm_reserve(struct vcm *vcm, size_t len, u32 attr);
/**
* vcm_reserve_at() - Make a reservation at a given logical location.
* @memtarget: A logical location to start the reservation from.
* @vcm: The VCM region to start the reservation from.
* @len: The length of the reservation.
* @attr: See 'Reservation Attributes'.
*
* The return value is non-zero if a reservation has been successfully
* created.
*/
struct res *vcm_reserve_at(enum memtarget_t memtarget, struct vcm *vcm,
size_t len, u32 attr);
/**
* vcm_get_vcm_from_res() - Return the VCM region of a reservation.
* @res: The reservation to return the VCM region of.
*
* Te return value will be non-zero if the reservation is valid. A valid
* reservation is always associated with a VCM region; there is no such
* thing as an orphan reservation.
*/
struct vcm *vcm_get_vcm_from_res(struct res *res);
/**
* vcm_unreserve() - Unreserve the reservation.
* @res: The reservation to unreserve.
*
* The return value will be 0 if the reservation was successfully
* unreserved and:
* -EBUSY The reservation is still backed,
* -EINVAL The vcm argument is invalid.
*/
int vcm_unreserve(struct res *res);
/**
* vcm_set_res_attr() - Set attributes of an existing reservation.
* @res: An existing reservation of interest.
* @attr: See 'Reservation Attributes'.
*
* This function can only be used on an existing reservation; there
* are no orphan reservations. All attributes can be set on a existing
* reservation.
*
* The return value will be 0 for a success, otherwise it will be:
* -EINVAL res or attr are invalid.
*/
int vcm_set_res_attr(struct res *res, u32 attr);
/**
* vcm_get_num_res() - Return the number of reservations in a VCM region.
* @vcm: The VCM region of interest.
*/
size_t vcm_get_num_res(struct vcm *vcm);
/**
* vcm_get_next_res() - Read each reservation one at a time.
* @vcm: The VCM region of interest.
* @res: Contains the last reservation. Pass NULL on the
* first call.
*
* This function works like a foreach reservation in a VCM region.
*
* The return value will be non-zero for each reservation in a VCM. A
* zero indicates no further reservations.
*/
struct res *vcm_get_next_res(struct vcm *vcm, struct res *res);
/**
* vcm_res_copy() - Copy len bytes from one reservation to another.
* @to: The reservation to copy to.
* @from: The reservation to copy from.
* @len: The length of bytes to copy.
*
* The return value is the number of bytes copied.
*/
size_t vcm_res_copy(struct res *to, size_t to_off, struct res *from, size_t
from_off, size_t len);
/**
* vcm_get_min_page_size() - Return the minimum page size supported by
* the architecture.
*/
size_t vcm_get_min_page_size(void);
/**
* vcm_back() - Physically back a reservation.
* @res: The reservation containing the virtual contiguous
* region to back.
* @physmem: The physical memory that will back the virtual
* contiguous memory region.
*
* One VCM can be associated with multiple devices. When you vcm_back()
* each association must be active. This is not strictly necessary. It may
* be changed in the future.
*
* This function returns 0 on a successful physical backing. Otherwise
* it returns:
* -EINVAL res or physmem is invalid or res's len
* is different from physmem's len.
* -EAGAIN Try again, one of the devices hasn't been activated.
*/
int vcm_back(struct res *res, struct physmem *physmem);
/**
* vcm_unback() - Unback a reservation.
* @res: The reservation to unback.
*
* One VCM can be associated with multiple devices. When you vcm_unback()
* each association must be active.
*
* This function returns 0 on a successful unbacking. Otherwise
* it returns:
* -EINVAL res is invalid.
* -EAGAIN Try again, one of the devices hasn't been activated.
*/
int vcm_unback(struct res *res);
/**
* vcm_phys_alloc() - Allocate physical memory for the VCM region.
* @memtype: The memory type to allocate.
* @len: The length of the allocation.
* @attr: See 'Physical Allocation Attributes'.
*
* This function will allocate chunks of memory according to the attr
* it is passed.
*
* The return value is non-zero if physical memory has been
* successfully allocated.
*/
struct physmem *vcm_phys_alloc(enum memtype_t memtype, size_t len, u32 attr);
/**
* vcm_phys_free() - Free a physical allocation.
* @physmem: The physical allocation to free.
*
* The return value is 0 if the physical allocation has been freed or:
* -EBUSY Their are reservation mapping the physical memory.
* -EINVAL The physmem argument is invalid.
*/
int vcm_phys_free(struct physmem *physmem);
/**
* vcm_get_physmem_from_res() - Return a reservation's physmem
* @res: An existing reservation of interest.
*
* The return value will be non-zero on success, otherwise it will be:
* -EINVAL res is invalid
* -ENOMEM res is unbacked
*/
struct physmem *vcm_get_physmem_from_res(struct res *res);
/**
* vcm_get_memtype_of_physalloc() - Return the memtype of a reservation.
* @physmem: The physical allocation of interest.
*
* This function returns the memtype of a reservation or VCM_INVALID
* if res is invalid.
*/
enum memtype_t vcm_get_memtype_of_physalloc(struct physmem *physmem);
/*
* Associate a VCM with a device, activate that association and remove it.
*
*/
/**
* vcm_assoc() - Associate a VCM with a device.
* @vcm: The VCM region of interest.
* @dev: The device to associate the VCM with.
* @attr: See 'Association Attributes'.
*
* This function returns non-zero if a association is made. It returns 0
* if any of its parameters are invalid or VCM_ATTR_VALID is not present.
*/
struct avcm *vcm_assoc(struct vcm *vcm, struct device *dev, u32 attr);
/**
* vcm_deassoc() - Deassociate a VCM from a device.
* @avcm: The association we want to break.
*
* The function returns 0 on success or:
* -EBUSY The association is currently activated.
* -EINVAL The avcm parameter is invalid.
*/
int vcm_deassoc(struct avcm *avcm);
/**
* vcm_set_assoc_attr() - Set an AVCM's attributes.
* @avcm: The AVCM of interest.
* @attr: The new attr. See 'Association Attributes'.
*
* Every attribute can be set at runtime if an association isn't activated.
*
* This function returns 0 on success or:
* -EBUSY The association is currently activated.
* -EINVAL The avcm parameter is invalid.
*/
int vcm_set_assoc_attr(struct avcm *avcm, u32 attr);
/**
* vcm_get_assoc_attr() - Return an AVCM's attributes.
* @avcm: The AVCM of interest.
*
* This function returns 0 on error.
*/
u32 vcm_get_assoc_attr(struct avcm *avcm);
/**
* vcm_activate() - Activate an AVCM.
* @avcm: The AVCM to activate.
*
* You have to deactivate, before you activate.
*
* This function returns 0 on success or:
* -EINVAL avcm is invalid
* -ENODEV no device
* -EBUSY device is already active
* -1 hardware failure
*/
int vcm_activate(struct avcm *avcm);
/**
* vcm_deactivate() - Deactivate an association.
* @avcm: The AVCM to deactivate.
*
* This function returns 0 on success or:
* -ENOENT avcm is not activate
* -EINVAL avcm is invalid
* -1 hardware failure
*/
int vcm_deactivate(struct avcm *avcm);
/**
* vcm_is_active() - Query if an AVCM is active.
* @avcm: The AVCM of interest.
*
* returns 0 for not active, 1 for active or -EINVAL for error.
*
*/
int vcm_is_active(struct avcm *avcm);
/*
* Create, manage and remove a boundary in a VCM.
*/
/**
* vcm_create_bound() - Create a bound in a VCM.
* @vcm: The VCM that needs a bound.
* @len: The len of the bound.
*
* The allocator picks the virtual addresses of the bound.
*
* This function returns non-zero if a bound was created.
*/
struct bound *vcm_create_bound(struct vcm *vcm, size_t len);
/**
* vcm_free_bound() - Free a bound.
* @bound: The bound to remove.
*
* This function returns 0 if bound has been removed or:
* -EBUSY The bound contains reservations and cannot be removed.
* -EINVAL The bound is invalid.
*/
int vcm_free_bound(struct bound *bound);
/**
* vcm_reserve_from_bound() - Make a reservation from a bounded area.
* @bound: The bound to reserve from.
* @len: The len of the reservation.
* @attr: See 'Reservation Attributes'.
*
* The return value is non-zero on success. It is 0 if any parameter
* is invalid.
*/
struct res *vcm_reserve_from_bound(struct bound *bound, size_t len,
u32 attr);
/**
* vcm_get_bound_start_addr() - Return the starting device address of the bound
* @bound: The bound of interest.
*
* On success this function returns the starting addres of the bound. On error
* it returns:
* 1 bound_id is invalid.
*/
size_t vcm_get_bound_start_addr(struct bound *bound);
/*
* Perform low-level control over VCM regions and reservations.
*/
/**
* vcm_map_phys_addr() - Produce a physmem from a contiguous
* physical address
*
* @phys: The physical address of the contiguous range.
* @len: The len of the contiguous address range.
*
* Returns non-zero on success, 0 on failure.
*/
struct physmem *vcm_map_phys_addr(phys_addr_t phys, size_t len);
/**
* vcm_get_next_phys_addr() - Get the next physical addr and len of a physmem.
* @physmem: The physmem of interest.
* @phys: The current physical address. Set this to NULL to
* start the iteration.
* @len An output: the len of the next physical segment.
*
* physmems may contain physically discontiguous sections. This
* function returns the next physical address and len. Pass NULL to
* phys to get the first physical address. The len of the physical
* segment is returned in *len.
*
* Returns 0 if there is no next physical address.
*/
size_t vcm_get_next_phys_addr(struct physmem *physmem, phys_addr_t phys,
size_t *len);
/**
* vcm_get_dev_addr() - Return the device address of a reservation.
* @res: The reservation of interest.
*
*
* On success this function returns the device address of a reservation. On
* error it returns:
* 1 res is invalid.
*
* Note: This may return a kernel address if the reservation was
* created from vcm_create_from_prebuilt() and the prebuilt ext_vcm_id
* references a VM page table.
*/
phys_addr_t vcm_get_dev_addr(struct res *res);
/**
* vcm_get_res() - Return the reservation from a device address and a VCM
* @dev_addr: The device address of interest.
* @vcm: The VCM that contains the reservation
*
* This function returns 0 if there is no reservation whose device
* address is dev_addr.
*/
struct res *vcm_get_res(unsigned long dev_addr, struct vcm *vcm);
/**
* vcm_translate() - Translate from one device address to another.
* @src_dev: The source device address.
* @src_vcm: The source VCM region.
* @dst_vcm: The destination VCM region.
*
* Derive the device address from a VCM region that maps the same physical
* memory as a device address from another VCM region.
*
* On success this function returns the device address of a translation. On
* error it returns:
* 1 res_id is invalid.
*/
size_t vcm_translate(struct device *src_dev, struct vcm *src_vcm,
struct vcm *dst_vcm);
/**
* vcm_get_phys_num_res() - Return the number of reservations mapping a
* physical address.
* @phys: The physical address to read.
*/
size_t vcm_get_phys_num_res(phys_addr_t phys);
/**
* vcm_get_next_phys_res() - Return the next reservation mapped to a physical
* address.
* @phys: The physical address to map.
* @res: The starting reservation. Set this to NULL for the first
* reservation.
* @len: The virtual length of the reservation
*
* This function returns 0 for the last reservation or no reservation.
*/
struct res *vcm_get_next_phys_res(phys_addr_t phys, struct res *res,
size_t *len);
/**
* vcm_get_pgtbl_pa() - Return the physcial address of a VCM's page table.
* @vcm: The VCM region of interest.
*
* This function returns non-zero on success.
*/
phys_addr_t vcm_get_pgtbl_pa(struct vcm *vcm);
/**
* vcm_get_cont_memtype_pa() - Return the phys base addr of a memtype's
* first contiguous region.
* @memtype: The memtype of interest.
*
* This function returns non-zero on success. A zero return indicates that
* the given memtype does not have a contiguous region or that the memtype
* is invalid.
*/
phys_addr_t vcm_get_cont_memtype_pa(enum memtype_t memtype);
/**
* vcm_get_cont_memtype_len() - Return the len of a memtype's
* first contiguous region.
* @memtype: The memtype of interest.
*
* This function returns non-zero on success. A zero return indicates that
* the given memtype does not have a contiguous region or that the memtype
* is invalid.
*/
size_t vcm_get_cont_memtype_len(enum memtype_t memtype);
/**
* vcm_dev_addr_to_phys_addr() - Perform a device address page-table lookup.
* @vcm: VCM to use for translation.
* @dev_addr: The device address to map.
*
* This function returns the pa of a va from a device's page-table. It will
* fault if the dev_addr is not mapped.
*/
phys_addr_t vcm_dev_addr_to_phys_addr(struct vcm *vcm, unsigned long dev_addr);
/*
* Fault Hooks
*
* vcm_hook()
*/
/**
* vcm_hook() - Add a fault handler.
* @dev: The device.
* @handler: The handler.
* @data: A private piece of data that will get passed to the
* handler.
*
* This function returns 0 for a successful registration or:
* -EINVAL The arguments are invalid.
*/
int vcm_hook(struct device *dev, vcm_handler handler, void *data);
/*
* Low level, platform agnostic, HW control.
*
* vcm_hw_ver()
*/
/**
* vcm_hw_ver() - Return the hardware version of a device, if it has one.
* @dev The device.
*/
size_t vcm_hw_ver(size_t dev);
#endif /* _VCM_H_ */
|