Re: [Intel-gfx] [RFC PATCH 03/42] drm/i915: buddy allocator

2019-02-18 Thread Jani Nikula
On Fri, 15 Feb 2019, Chris Wilson  wrote:
> Quoting Jani Nikula (2019-02-15 12:34:02)
>> Please replace the above with
>> 
>> // SPDX-License-Identifier: MIT
>> /*
>>  * Copyright © 2019 Intel Corporation
>>  */
>> 
>> Ditto for all new files being added. (Except .h which will need to have
>> old style /* ... */ comments around the first line.)
>
> Rebel against the overlords imposing C++ comments upon us; we will
> remain clean! I really do not see the point in using C++ for this one
> instance, the perl regex is not any harder to write...

In this case, I'd rather not leave people any wiggle room. Accept any
deviation, and it'll get copy-pasted and deviated more. I want the
message to be clear, put this in verbatim and don't come up with clever
ideas. (Yeah, I see there's deviation already, and I've already
contemplated patches to fix them.)

Perhaps my sentiments are amplified by my annoyance at the combinatorial
explosion of subtly different MIT license texts we already have, making
what's supposed to be a trivial switch to SPDX rather more complicated.

BR,
Jani.


-- 
Jani Nikula, Intel Open Source Graphics Center
___
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

Re: [Intel-gfx] [RFC PATCH 03/42] drm/i915: buddy allocator

2019-02-15 Thread Chris Wilson
Quoting Jani Nikula (2019-02-15 12:34:02)
> Please replace the above with
> 
> // SPDX-License-Identifier: MIT
> /*
>  * Copyright © 2019 Intel Corporation
>  */
> 
> Ditto for all new files being added. (Except .h which will need to have
> old style /* ... */ comments around the first line.)

Rebel against the overlords imposing C++ comments upon us; we will
remain clean! I really do not see the point in using C++ for this one
instance, the perl regex is not any harder to write...
-Chris
___
Intel-gfx mailing list
Intel-gfx@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/intel-gfx

Re: [Intel-gfx] [RFC PATCH 03/42] drm/i915: buddy allocator

2019-02-15 Thread Jani Nikula
On Thu, 14 Feb 2019, Matthew Auld  wrote:
> Really simply buddy allocator.
>
> Signed-off-by: Matthew Auld 
> Cc: Joonas Lahtinen 
> Cc: Abdiel Janulgue 
> ---
>  drivers/gpu/drm/i915/Makefile |   1 +
>  drivers/gpu/drm/i915/i915_gem_buddy.c | 206 +
>  drivers/gpu/drm/i915/i915_gem_buddy.h | 118 ++
>  .../gpu/drm/i915/selftests/i915_gem_buddy.c   | 209 ++
>  .../drm/i915/selftests/i915_mock_selftests.h  |   1 +
>  5 files changed, 535 insertions(+)
>  create mode 100644 drivers/gpu/drm/i915/i915_gem_buddy.c
>  create mode 100644 drivers/gpu/drm/i915/i915_gem_buddy.h
>  create mode 100644 drivers/gpu/drm/i915/selftests/i915_gem_buddy.c
>
> diff --git a/drivers/gpu/drm/i915/Makefile b/drivers/gpu/drm/i915/Makefile
> index 1787e1299b1b..e5ce813d1936 100644
> --- a/drivers/gpu/drm/i915/Makefile
> +++ b/drivers/gpu/drm/i915/Makefile
> @@ -61,6 +61,7 @@ i915-y += \
> i915_active.o \
> i915_cmd_parser.o \
> i915_gem_batch_pool.o \
> +   i915_gem_buddy.o \
> i915_gem_clflush.o \
> i915_gem_context.o \
> i915_gem_dmabuf.o \
> diff --git a/drivers/gpu/drm/i915/i915_gem_buddy.c 
> b/drivers/gpu/drm/i915/i915_gem_buddy.c
> new file mode 100644
> index ..4dc688c091a2
> --- /dev/null
> +++ b/drivers/gpu/drm/i915/i915_gem_buddy.c
> @@ -0,0 +1,206 @@
> +/*
> + * Copyright © 2018 Intel Corporation
> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a
> + * copy of this software and associated documentation files (the "Software"),
> + * to deal in the Software without restriction, including without limitation
> + * the rights to use, copy, modify, merge, publish, distribute, sublicense,
> + * and/or sell copies of the Software, and to permit persons to whom the
> + * Software is furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice (including the next
> + * paragraph) shall be included in all copies or substantial portions of the
> + * Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
> + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
> DEALINGS
> + * IN THE SOFTWARE.
> + *
> + */

Please replace the above with

// SPDX-License-Identifier: MIT
/*
 * Copyright © 2019 Intel Corporation
 */

Ditto for all new files being added. (Except .h which will need to have
old style /* ... */ comments around the first line.)

BR,
Jani.


> +
> +#include 
> +#include 
> +
> +#include "i915_gem_buddy.h"
> +#include "i915_gem.h"
> +
> +int i915_gem_buddy_init(struct i915_gem_buddy_mm *mm, u64 size, u64 min_size)
> +{
> + unsigned int i;
> +
> + /*
> +  * XXX: if not a power of 2, maybe split into power of 2 blocks,
> +  * effectively having multiple roots, similar to if we had a global
> +  * MAX_ORDER.
> +  */
> + size = rounddown_pow_of_two(size);
> + min_size = roundup_pow_of_two(min_size);
> +
> + if (size < min_size)
> + return -EINVAL;
> +
> + if (min_size < PAGE_SIZE)
> + return -EINVAL;
> +
> + mm->max_order = ilog2(size) - ilog2(min_size);
> + mm->min_size = min_size;
> +
> + mm->free_list = kmalloc_array(mm->max_order + 1,
> +   sizeof(struct list_head),
> +   GFP_KERNEL);
> + if (!mm->free_list)
> + return -ENOMEM;
> +
> + for (i = 0; i <= mm->max_order; ++i)
> + INIT_LIST_HEAD(>free_list[i]);
> +
> + mm->blocks = KMEM_CACHE(i915_gem_buddy_block, SLAB_HWCACHE_ALIGN);
> + if (!mm->blocks)
> + goto out_free_list;
> +
> + mm->root = kmem_cache_zalloc(mm->blocks, GFP_KERNEL);
> + if (!mm->root)
> + goto out_free_blocks;
> +
> + mm->root->header = mm->max_order;
> +
> + list_add(>root->link, >free_list[mm->max_order]);
> +
> + return 0;
> +
> +out_free_blocks:
> + kmem_cache_destroy(mm->blocks);
> +out_free_list:
> + kfree(mm->free_list);
> +
> + return -ENOMEM;
> +}
> +
> +void i915_gem_buddy_fini(struct i915_gem_buddy_mm *mm)
> +{
> + if (WARN_ON(i915_gem_buddy_block_allocated(mm->root)))
> + return;
> +
> + kfree(mm->free_list);
> + kmem_cache_free(mm->blocks, mm->root);
> + kmem_cache_destroy(mm->blocks);
> +}
> +
> +/*
> + * The 'order' here means:
> + *
> + * 0 = 2^0 * mm->min_size
> + * 1 = 2^1 * mm->min_size
> + * 2 = 2^2 * mm->min_size
> + * ...
> + */
> +struct i915_gem_buddy_block *
> +i915_gem_buddy_alloc(struct i915_gem_buddy_mm *mm, unsigned int 

[Intel-gfx] [RFC PATCH 03/42] drm/i915: buddy allocator

2019-02-14 Thread Matthew Auld
Really simply buddy allocator.

Signed-off-by: Matthew Auld 
Cc: Joonas Lahtinen 
Cc: Abdiel Janulgue 
---
 drivers/gpu/drm/i915/Makefile |   1 +
 drivers/gpu/drm/i915/i915_gem_buddy.c | 206 +
 drivers/gpu/drm/i915/i915_gem_buddy.h | 118 ++
 .../gpu/drm/i915/selftests/i915_gem_buddy.c   | 209 ++
 .../drm/i915/selftests/i915_mock_selftests.h  |   1 +
 5 files changed, 535 insertions(+)
 create mode 100644 drivers/gpu/drm/i915/i915_gem_buddy.c
 create mode 100644 drivers/gpu/drm/i915/i915_gem_buddy.h
 create mode 100644 drivers/gpu/drm/i915/selftests/i915_gem_buddy.c

diff --git a/drivers/gpu/drm/i915/Makefile b/drivers/gpu/drm/i915/Makefile
index 1787e1299b1b..e5ce813d1936 100644
--- a/drivers/gpu/drm/i915/Makefile
+++ b/drivers/gpu/drm/i915/Makefile
@@ -61,6 +61,7 @@ i915-y += \
  i915_active.o \
  i915_cmd_parser.o \
  i915_gem_batch_pool.o \
+ i915_gem_buddy.o \
  i915_gem_clflush.o \
  i915_gem_context.o \
  i915_gem_dmabuf.o \
diff --git a/drivers/gpu/drm/i915/i915_gem_buddy.c 
b/drivers/gpu/drm/i915/i915_gem_buddy.c
new file mode 100644
index ..4dc688c091a2
--- /dev/null
+++ b/drivers/gpu/drm/i915/i915_gem_buddy.c
@@ -0,0 +1,206 @@
+/*
+ * Copyright © 2018 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#include 
+#include 
+
+#include "i915_gem_buddy.h"
+#include "i915_gem.h"
+
+int i915_gem_buddy_init(struct i915_gem_buddy_mm *mm, u64 size, u64 min_size)
+{
+   unsigned int i;
+
+   /*
+* XXX: if not a power of 2, maybe split into power of 2 blocks,
+* effectively having multiple roots, similar to if we had a global
+* MAX_ORDER.
+*/
+   size = rounddown_pow_of_two(size);
+   min_size = roundup_pow_of_two(min_size);
+
+   if (size < min_size)
+   return -EINVAL;
+
+   if (min_size < PAGE_SIZE)
+   return -EINVAL;
+
+   mm->max_order = ilog2(size) - ilog2(min_size);
+   mm->min_size = min_size;
+
+   mm->free_list = kmalloc_array(mm->max_order + 1,
+ sizeof(struct list_head),
+ GFP_KERNEL);
+   if (!mm->free_list)
+   return -ENOMEM;
+
+   for (i = 0; i <= mm->max_order; ++i)
+   INIT_LIST_HEAD(>free_list[i]);
+
+   mm->blocks = KMEM_CACHE(i915_gem_buddy_block, SLAB_HWCACHE_ALIGN);
+   if (!mm->blocks)
+   goto out_free_list;
+
+   mm->root = kmem_cache_zalloc(mm->blocks, GFP_KERNEL);
+   if (!mm->root)
+   goto out_free_blocks;
+
+   mm->root->header = mm->max_order;
+
+   list_add(>root->link, >free_list[mm->max_order]);
+
+   return 0;
+
+out_free_blocks:
+   kmem_cache_destroy(mm->blocks);
+out_free_list:
+   kfree(mm->free_list);
+
+   return -ENOMEM;
+}
+
+void i915_gem_buddy_fini(struct i915_gem_buddy_mm *mm)
+{
+   if (WARN_ON(i915_gem_buddy_block_allocated(mm->root)))
+   return;
+
+   kfree(mm->free_list);
+   kmem_cache_free(mm->blocks, mm->root);
+   kmem_cache_destroy(mm->blocks);
+}
+
+/*
+ * The 'order' here means:
+ *
+ * 0 = 2^0 * mm->min_size
+ * 1 = 2^1 * mm->min_size
+ * 2 = 2^2 * mm->min_size
+ * ...
+ */
+struct i915_gem_buddy_block *
+i915_gem_buddy_alloc(struct i915_gem_buddy_mm *mm, unsigned int order)
+{
+   struct i915_gem_buddy_block *block = NULL;
+   struct i915_gem_buddy_block *root;
+   unsigned int i;
+
+   for (i = order; i <= mm->max_order; ++i) {
+   block = list_first_entry_or_null(>free_list[i],
+struct i915_gem_buddy_block,
+link);
+   if (block)
+   break;
+   }
+
+   if (!block)
+