Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 Red Hat Inc. |
| 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 shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 | * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 | * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 | * OTHER DEALINGS IN THE SOFTWARE. |
| 21 | * |
| 22 | * Authors: Ben Skeggs |
| 23 | */ |
| 24 | |
| 25 | #include "drmP.h" |
| 26 | #include "nouveau_drv.h" |
| 27 | #include "nouveau_mm.h" |
| 28 | |
| 29 | static inline void |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 30 | region_put(struct nouveau_mm *mm, struct nouveau_mm_node *a) |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 31 | { |
| 32 | list_del(&a->nl_entry); |
| 33 | list_del(&a->fl_entry); |
| 34 | kfree(a); |
| 35 | } |
| 36 | |
| 37 | static struct nouveau_mm_node * |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 38 | region_split(struct nouveau_mm *mm, struct nouveau_mm_node *a, u32 size) |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 39 | { |
| 40 | struct nouveau_mm_node *b; |
| 41 | |
| 42 | if (a->length == size) |
| 43 | return a; |
| 44 | |
| 45 | b = kmalloc(sizeof(*b), GFP_KERNEL); |
| 46 | if (unlikely(b == NULL)) |
| 47 | return NULL; |
| 48 | |
| 49 | b->offset = a->offset; |
| 50 | b->length = size; |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 51 | b->type = a->type; |
| 52 | a->offset += size; |
| 53 | a->length -= size; |
| 54 | list_add_tail(&b->nl_entry, &a->nl_entry); |
Ben Skeggs | 8b464bf | 2011-01-14 15:46:30 +1000 | [diff] [blame] | 55 | if (b->type == 0) |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 56 | list_add_tail(&b->fl_entry, &a->fl_entry); |
| 57 | return b; |
| 58 | } |
| 59 | |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 60 | #define node(root, dir) ((root)->nl_entry.dir == &mm->nodes) ? NULL : \ |
Ben Skeggs | 8b464bf | 2011-01-14 15:46:30 +1000 | [diff] [blame] | 61 | list_entry((root)->nl_entry.dir, struct nouveau_mm_node, nl_entry) |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 62 | |
| 63 | void |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 64 | nouveau_mm_put(struct nouveau_mm *mm, struct nouveau_mm_node *this) |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 65 | { |
Ben Skeggs | 8b464bf | 2011-01-14 15:46:30 +1000 | [diff] [blame] | 66 | struct nouveau_mm_node *prev = node(this, prev); |
| 67 | struct nouveau_mm_node *next = node(this, next); |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 68 | |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 69 | list_add(&this->fl_entry, &mm->free); |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 70 | this->type = 0; |
Ben Skeggs | 8b464bf | 2011-01-14 15:46:30 +1000 | [diff] [blame] | 71 | |
| 72 | if (prev && prev->type == 0) { |
| 73 | prev->length += this->length; |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 74 | region_put(mm, this); |
Ben Skeggs | 8b464bf | 2011-01-14 15:46:30 +1000 | [diff] [blame] | 75 | this = prev; |
| 76 | } |
| 77 | |
| 78 | if (next && next->type == 0) { |
| 79 | next->offset = this->offset; |
| 80 | next->length += this->length; |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 81 | region_put(mm, this); |
Ben Skeggs | 8b464bf | 2011-01-14 15:46:30 +1000 | [diff] [blame] | 82 | } |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 86 | nouveau_mm_get(struct nouveau_mm *mm, int type, u32 size, u32 size_nc, |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 87 | u32 align, struct nouveau_mm_node **pnode) |
| 88 | { |
Ben Skeggs | 8b464bf | 2011-01-14 15:46:30 +1000 | [diff] [blame] | 89 | struct nouveau_mm_node *prev, *this, *next; |
| 90 | u32 min = size_nc ? size_nc : size; |
| 91 | u32 align_mask = align - 1; |
| 92 | u32 splitoff; |
| 93 | u32 s, e; |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 94 | |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 95 | list_for_each_entry(this, &mm->free, fl_entry) { |
Ben Skeggs | 8b464bf | 2011-01-14 15:46:30 +1000 | [diff] [blame] | 96 | e = this->offset + this->length; |
| 97 | s = this->offset; |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 98 | |
Ben Skeggs | 8b464bf | 2011-01-14 15:46:30 +1000 | [diff] [blame] | 99 | prev = node(this, prev); |
| 100 | if (prev && prev->type != type) |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 101 | s = roundup(s, mm->block_size); |
Ben Skeggs | 8b464bf | 2011-01-14 15:46:30 +1000 | [diff] [blame] | 102 | |
| 103 | next = node(this, next); |
| 104 | if (next && next->type != type) |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 105 | e = rounddown(e, mm->block_size); |
Ben Skeggs | 8b464bf | 2011-01-14 15:46:30 +1000 | [diff] [blame] | 106 | |
| 107 | s = (s + align_mask) & ~align_mask; |
| 108 | e &= ~align_mask; |
| 109 | if (s > e || e - s < min) |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 110 | continue; |
| 111 | |
Ben Skeggs | 8b464bf | 2011-01-14 15:46:30 +1000 | [diff] [blame] | 112 | splitoff = s - this->offset; |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 113 | if (splitoff && !region_split(mm, this, splitoff)) |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 114 | return -ENOMEM; |
| 115 | |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 116 | this = region_split(mm, this, min(size, e - s)); |
Ben Skeggs | 8b464bf | 2011-01-14 15:46:30 +1000 | [diff] [blame] | 117 | if (!this) |
| 118 | return -ENOMEM; |
| 119 | |
| 120 | this->type = type; |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 121 | list_del(&this->fl_entry); |
| 122 | *pnode = this; |
| 123 | return 0; |
| 124 | } |
| 125 | |
Ben Skeggs | ef1b287 | 2011-03-07 17:18:03 +1000 | [diff] [blame] | 126 | return -ENOSPC; |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | int |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 130 | nouveau_mm_init(struct nouveau_mm *mm, u32 offset, u32 length, u32 block) |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 131 | { |
Ben Skeggs | a12036b | 2011-06-24 10:23:20 +1000 | [diff] [blame^] | 132 | struct nouveau_mm_node *node; |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 133 | |
Ben Skeggs | a12036b | 2011-06-24 10:23:20 +1000 | [diff] [blame^] | 134 | if (block) { |
| 135 | mutex_init(&mm->mutex); |
| 136 | INIT_LIST_HEAD(&mm->nodes); |
| 137 | INIT_LIST_HEAD(&mm->free); |
| 138 | mm->block_size = block; |
| 139 | mm->heap_nodes = 0; |
| 140 | } |
| 141 | |
| 142 | node = kzalloc(sizeof(*node), GFP_KERNEL); |
| 143 | if (!node) |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 144 | return -ENOMEM; |
Ben Skeggs | a12036b | 2011-06-24 10:23:20 +1000 | [diff] [blame^] | 145 | node->offset = roundup(offset, mm->block_size); |
| 146 | node->length = rounddown(offset + length, mm->block_size) - node->offset; |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 147 | |
Ben Skeggs | a12036b | 2011-06-24 10:23:20 +1000 | [diff] [blame^] | 148 | list_add_tail(&node->nl_entry, &mm->nodes); |
| 149 | list_add_tail(&node->fl_entry, &mm->free); |
| 150 | mm->heap_nodes++; |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | int |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 155 | nouveau_mm_fini(struct nouveau_mm *mm) |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 156 | { |
Ben Skeggs | ad9ac43 | 2011-06-07 16:18:19 +1000 | [diff] [blame] | 157 | struct nouveau_mm_node *node, *heap = |
Ben Skeggs | 987eec1 | 2011-06-24 10:14:07 +1000 | [diff] [blame] | 158 | list_first_entry(&mm->nodes, struct nouveau_mm_node, nl_entry); |
Ben Skeggs | a12036b | 2011-06-24 10:23:20 +1000 | [diff] [blame^] | 159 | int nodes = 0; |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 160 | |
Ben Skeggs | a12036b | 2011-06-24 10:23:20 +1000 | [diff] [blame^] | 161 | list_for_each_entry(node, &mm->nodes, nl_entry) { |
| 162 | if (nodes++ == mm->heap_nodes) { |
| 163 | printk(KERN_ERR "nouveau_mm in use at destroy time!\n"); |
| 164 | list_for_each_entry(node, &mm->nodes, nl_entry) { |
| 165 | printk(KERN_ERR "0x%02x: 0x%08x 0x%08x\n", |
| 166 | node->type, node->offset, node->length); |
| 167 | } |
| 168 | WARN_ON(1); |
| 169 | return -EBUSY; |
Ben Skeggs | ad9ac43 | 2011-06-07 16:18:19 +1000 | [diff] [blame] | 170 | } |
Ben Skeggs | ad9ac43 | 2011-06-07 16:18:19 +1000 | [diff] [blame] | 171 | } |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 172 | |
| 173 | kfree(heap); |
Ben Skeggs | 573a2a3 | 2010-08-25 15:26:04 +1000 | [diff] [blame] | 174 | return 0; |
| 175 | } |