i915_gem.c 125 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987
  1. /*
  2. * Copyright © 2008 Intel Corporation
  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 (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/shmem_fs.h>
  34. #include <linux/slab.h>
  35. #include <linux/swap.h>
  36. #include <linux/pci.h>
  37. #include <linux/dma-buf.h>
  38. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  39. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj,
  40. bool force);
  41. static __must_check int
  42. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  43. bool readonly);
  44. static __must_check int
  45. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  46. struct i915_address_space *vm,
  47. unsigned alignment,
  48. bool map_and_fenceable,
  49. bool nonblocking);
  50. static int i915_gem_phys_pwrite(struct drm_device *dev,
  51. struct drm_i915_gem_object *obj,
  52. struct drm_i915_gem_pwrite *args,
  53. struct drm_file *file);
  54. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  55. struct drm_i915_gem_object *obj);
  56. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  57. struct drm_i915_fence_reg *fence,
  58. bool enable);
  59. static unsigned long i915_gem_inactive_count(struct shrinker *shrinker,
  60. struct shrink_control *sc);
  61. static unsigned long i915_gem_inactive_scan(struct shrinker *shrinker,
  62. struct shrink_control *sc);
  63. static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
  64. static long i915_gem_shrink_all(struct drm_i915_private *dev_priv);
  65. static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
  66. static bool cpu_cache_is_coherent(struct drm_device *dev,
  67. enum i915_cache_level level)
  68. {
  69. return HAS_LLC(dev) || level != I915_CACHE_NONE;
  70. }
  71. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  72. {
  73. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  74. return true;
  75. return obj->pin_display;
  76. }
  77. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  78. {
  79. if (obj->tiling_mode)
  80. i915_gem_release_mmap(obj);
  81. /* As we do not have an associated fence register, we will force
  82. * a tiling change if we ever need to acquire one.
  83. */
  84. obj->fence_dirty = false;
  85. obj->fence_reg = I915_FENCE_REG_NONE;
  86. }
  87. /* some bookkeeping */
  88. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  89. size_t size)
  90. {
  91. spin_lock(&dev_priv->mm.object_stat_lock);
  92. dev_priv->mm.object_count++;
  93. dev_priv->mm.object_memory += size;
  94. spin_unlock(&dev_priv->mm.object_stat_lock);
  95. }
  96. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  97. size_t size)
  98. {
  99. spin_lock(&dev_priv->mm.object_stat_lock);
  100. dev_priv->mm.object_count--;
  101. dev_priv->mm.object_memory -= size;
  102. spin_unlock(&dev_priv->mm.object_stat_lock);
  103. }
  104. static int
  105. i915_gem_wait_for_error(struct i915_gpu_error *error)
  106. {
  107. int ret;
  108. #define EXIT_COND (!i915_reset_in_progress(error) || \
  109. i915_terminally_wedged(error))
  110. if (EXIT_COND)
  111. return 0;
  112. /*
  113. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  114. * userspace. If it takes that long something really bad is going on and
  115. * we should simply try to bail out and fail as gracefully as possible.
  116. */
  117. ret = wait_event_interruptible_timeout(error->reset_queue,
  118. EXIT_COND,
  119. 10*HZ);
  120. if (ret == 0) {
  121. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  122. return -EIO;
  123. } else if (ret < 0) {
  124. return ret;
  125. }
  126. #undef EXIT_COND
  127. return 0;
  128. }
  129. int i915_mutex_lock_interruptible(struct drm_device *dev)
  130. {
  131. struct drm_i915_private *dev_priv = dev->dev_private;
  132. int ret;
  133. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  134. if (ret)
  135. return ret;
  136. ret = mutex_lock_interruptible(&dev->struct_mutex);
  137. if (ret)
  138. return ret;
  139. WARN_ON(i915_verify_lists(dev));
  140. return 0;
  141. }
  142. static inline bool
  143. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
  144. {
  145. return i915_gem_obj_bound_any(obj) && !obj->active;
  146. }
  147. int
  148. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  149. struct drm_file *file)
  150. {
  151. struct drm_i915_private *dev_priv = dev->dev_private;
  152. struct drm_i915_gem_init *args = data;
  153. if (drm_core_check_feature(dev, DRIVER_MODESET))
  154. return -ENODEV;
  155. if (args->gtt_start >= args->gtt_end ||
  156. (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
  157. return -EINVAL;
  158. /* GEM with user mode setting was never supported on ilk and later. */
  159. if (INTEL_INFO(dev)->gen >= 5)
  160. return -ENODEV;
  161. mutex_lock(&dev->struct_mutex);
  162. i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end,
  163. args->gtt_end);
  164. dev_priv->gtt.mappable_end = args->gtt_end;
  165. mutex_unlock(&dev->struct_mutex);
  166. return 0;
  167. }
  168. int
  169. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  170. struct drm_file *file)
  171. {
  172. struct drm_i915_private *dev_priv = dev->dev_private;
  173. struct drm_i915_gem_get_aperture *args = data;
  174. struct drm_i915_gem_object *obj;
  175. size_t pinned;
  176. pinned = 0;
  177. mutex_lock(&dev->struct_mutex);
  178. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  179. if (obj->pin_count)
  180. pinned += i915_gem_obj_ggtt_size(obj);
  181. mutex_unlock(&dev->struct_mutex);
  182. args->aper_size = dev_priv->gtt.base.total;
  183. args->aper_available_size = args->aper_size - pinned;
  184. return 0;
  185. }
  186. void *i915_gem_object_alloc(struct drm_device *dev)
  187. {
  188. struct drm_i915_private *dev_priv = dev->dev_private;
  189. return kmem_cache_zalloc(dev_priv->slab, GFP_KERNEL);
  190. }
  191. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  192. {
  193. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  194. kmem_cache_free(dev_priv->slab, obj);
  195. }
  196. static int
  197. i915_gem_create(struct drm_file *file,
  198. struct drm_device *dev,
  199. uint64_t size,
  200. uint32_t *handle_p)
  201. {
  202. struct drm_i915_gem_object *obj;
  203. int ret;
  204. u32 handle;
  205. size = roundup(size, PAGE_SIZE);
  206. if (size == 0)
  207. return -EINVAL;
  208. /* Allocate the new object */
  209. obj = i915_gem_alloc_object(dev, size);
  210. if (obj == NULL)
  211. return -ENOMEM;
  212. ret = drm_gem_handle_create(file, &obj->base, &handle);
  213. /* drop reference from allocate - handle holds it now */
  214. drm_gem_object_unreference_unlocked(&obj->base);
  215. if (ret)
  216. return ret;
  217. *handle_p = handle;
  218. return 0;
  219. }
  220. int
  221. i915_gem_dumb_create(struct drm_file *file,
  222. struct drm_device *dev,
  223. struct drm_mode_create_dumb *args)
  224. {
  225. /* have to work out size/pitch and return them */
  226. args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
  227. args->size = args->pitch * args->height;
  228. return i915_gem_create(file, dev,
  229. args->size, &args->handle);
  230. }
  231. /**
  232. * Creates a new mm object and returns a handle to it.
  233. */
  234. int
  235. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  236. struct drm_file *file)
  237. {
  238. struct drm_i915_gem_create *args = data;
  239. return i915_gem_create(file, dev,
  240. args->size, &args->handle);
  241. }
  242. static inline int
  243. __copy_to_user_swizzled(char __user *cpu_vaddr,
  244. const char *gpu_vaddr, int gpu_offset,
  245. int length)
  246. {
  247. int ret, cpu_offset = 0;
  248. while (length > 0) {
  249. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  250. int this_length = min(cacheline_end - gpu_offset, length);
  251. int swizzled_gpu_offset = gpu_offset ^ 64;
  252. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  253. gpu_vaddr + swizzled_gpu_offset,
  254. this_length);
  255. if (ret)
  256. return ret + length;
  257. cpu_offset += this_length;
  258. gpu_offset += this_length;
  259. length -= this_length;
  260. }
  261. return 0;
  262. }
  263. static inline int
  264. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  265. const char __user *cpu_vaddr,
  266. int length)
  267. {
  268. int ret, cpu_offset = 0;
  269. while (length > 0) {
  270. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  271. int this_length = min(cacheline_end - gpu_offset, length);
  272. int swizzled_gpu_offset = gpu_offset ^ 64;
  273. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  274. cpu_vaddr + cpu_offset,
  275. this_length);
  276. if (ret)
  277. return ret + length;
  278. cpu_offset += this_length;
  279. gpu_offset += this_length;
  280. length -= this_length;
  281. }
  282. return 0;
  283. }
  284. /* Per-page copy function for the shmem pread fastpath.
  285. * Flushes invalid cachelines before reading the target if
  286. * needs_clflush is set. */
  287. static int
  288. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  289. char __user *user_data,
  290. bool page_do_bit17_swizzling, bool needs_clflush)
  291. {
  292. char *vaddr;
  293. int ret;
  294. if (unlikely(page_do_bit17_swizzling))
  295. return -EINVAL;
  296. vaddr = kmap_atomic(page);
  297. if (needs_clflush)
  298. drm_clflush_virt_range(vaddr + shmem_page_offset,
  299. page_length);
  300. ret = __copy_to_user_inatomic(user_data,
  301. vaddr + shmem_page_offset,
  302. page_length);
  303. kunmap_atomic(vaddr);
  304. return ret ? -EFAULT : 0;
  305. }
  306. static void
  307. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  308. bool swizzled)
  309. {
  310. if (unlikely(swizzled)) {
  311. unsigned long start = (unsigned long) addr;
  312. unsigned long end = (unsigned long) addr + length;
  313. /* For swizzling simply ensure that we always flush both
  314. * channels. Lame, but simple and it works. Swizzled
  315. * pwrite/pread is far from a hotpath - current userspace
  316. * doesn't use it at all. */
  317. start = round_down(start, 128);
  318. end = round_up(end, 128);
  319. drm_clflush_virt_range((void *)start, end - start);
  320. } else {
  321. drm_clflush_virt_range(addr, length);
  322. }
  323. }
  324. /* Only difference to the fast-path function is that this can handle bit17
  325. * and uses non-atomic copy and kmap functions. */
  326. static int
  327. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  328. char __user *user_data,
  329. bool page_do_bit17_swizzling, bool needs_clflush)
  330. {
  331. char *vaddr;
  332. int ret;
  333. vaddr = kmap(page);
  334. if (needs_clflush)
  335. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  336. page_length,
  337. page_do_bit17_swizzling);
  338. if (page_do_bit17_swizzling)
  339. ret = __copy_to_user_swizzled(user_data,
  340. vaddr, shmem_page_offset,
  341. page_length);
  342. else
  343. ret = __copy_to_user(user_data,
  344. vaddr + shmem_page_offset,
  345. page_length);
  346. kunmap(page);
  347. return ret ? - EFAULT : 0;
  348. }
  349. static int
  350. i915_gem_shmem_pread(struct drm_device *dev,
  351. struct drm_i915_gem_object *obj,
  352. struct drm_i915_gem_pread *args,
  353. struct drm_file *file)
  354. {
  355. char __user *user_data;
  356. ssize_t remain;
  357. loff_t offset;
  358. int shmem_page_offset, page_length, ret = 0;
  359. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  360. int prefaulted = 0;
  361. int needs_clflush = 0;
  362. struct sg_page_iter sg_iter;
  363. user_data = to_user_ptr(args->data_ptr);
  364. remain = args->size;
  365. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  366. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  367. /* If we're not in the cpu read domain, set ourself into the gtt
  368. * read domain and manually flush cachelines (if required). This
  369. * optimizes for the case when the gpu will dirty the data
  370. * anyway again before the next pread happens. */
  371. needs_clflush = !cpu_cache_is_coherent(dev, obj->cache_level);
  372. ret = i915_gem_object_wait_rendering(obj, true);
  373. if (ret)
  374. return ret;
  375. }
  376. ret = i915_gem_object_get_pages(obj);
  377. if (ret)
  378. return ret;
  379. i915_gem_object_pin_pages(obj);
  380. offset = args->offset;
  381. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  382. offset >> PAGE_SHIFT) {
  383. struct page *page = sg_page_iter_page(&sg_iter);
  384. if (remain <= 0)
  385. break;
  386. /* Operation in this page
  387. *
  388. * shmem_page_offset = offset within page in shmem file
  389. * page_length = bytes to copy for this page
  390. */
  391. shmem_page_offset = offset_in_page(offset);
  392. page_length = remain;
  393. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  394. page_length = PAGE_SIZE - shmem_page_offset;
  395. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  396. (page_to_phys(page) & (1 << 17)) != 0;
  397. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  398. user_data, page_do_bit17_swizzling,
  399. needs_clflush);
  400. if (ret == 0)
  401. goto next_page;
  402. mutex_unlock(&dev->struct_mutex);
  403. if (likely(!i915_prefault_disable) && !prefaulted) {
  404. ret = fault_in_multipages_writeable(user_data, remain);
  405. /* Userspace is tricking us, but we've already clobbered
  406. * its pages with the prefault and promised to write the
  407. * data up to the first fault. Hence ignore any errors
  408. * and just continue. */
  409. (void)ret;
  410. prefaulted = 1;
  411. }
  412. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  413. user_data, page_do_bit17_swizzling,
  414. needs_clflush);
  415. mutex_lock(&dev->struct_mutex);
  416. next_page:
  417. mark_page_accessed(page);
  418. if (ret)
  419. goto out;
  420. remain -= page_length;
  421. user_data += page_length;
  422. offset += page_length;
  423. }
  424. out:
  425. i915_gem_object_unpin_pages(obj);
  426. return ret;
  427. }
  428. /**
  429. * Reads data from the object referenced by handle.
  430. *
  431. * On error, the contents of *data are undefined.
  432. */
  433. int
  434. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  435. struct drm_file *file)
  436. {
  437. struct drm_i915_gem_pread *args = data;
  438. struct drm_i915_gem_object *obj;
  439. int ret = 0;
  440. if (args->size == 0)
  441. return 0;
  442. if (!access_ok(VERIFY_WRITE,
  443. to_user_ptr(args->data_ptr),
  444. args->size))
  445. return -EFAULT;
  446. ret = i915_mutex_lock_interruptible(dev);
  447. if (ret)
  448. return ret;
  449. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  450. if (&obj->base == NULL) {
  451. ret = -ENOENT;
  452. goto unlock;
  453. }
  454. /* Bounds check source. */
  455. if (args->offset > obj->base.size ||
  456. args->size > obj->base.size - args->offset) {
  457. ret = -EINVAL;
  458. goto out;
  459. }
  460. /* prime objects have no backing filp to GEM pread/pwrite
  461. * pages from.
  462. */
  463. if (!obj->base.filp) {
  464. ret = -EINVAL;
  465. goto out;
  466. }
  467. trace_i915_gem_object_pread(obj, args->offset, args->size);
  468. ret = i915_gem_shmem_pread(dev, obj, args, file);
  469. out:
  470. drm_gem_object_unreference(&obj->base);
  471. unlock:
  472. mutex_unlock(&dev->struct_mutex);
  473. return ret;
  474. }
  475. /* This is the fast write path which cannot handle
  476. * page faults in the source data
  477. */
  478. static inline int
  479. fast_user_write(struct io_mapping *mapping,
  480. loff_t page_base, int page_offset,
  481. char __user *user_data,
  482. int length)
  483. {
  484. void __iomem *vaddr_atomic;
  485. void *vaddr;
  486. unsigned long unwritten;
  487. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  488. /* We can use the cpu mem copy function because this is X86. */
  489. vaddr = (void __force*)vaddr_atomic + page_offset;
  490. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  491. user_data, length);
  492. io_mapping_unmap_atomic(vaddr_atomic);
  493. return unwritten;
  494. }
  495. /**
  496. * This is the fast pwrite path, where we copy the data directly from the
  497. * user into the GTT, uncached.
  498. */
  499. static int
  500. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  501. struct drm_i915_gem_object *obj,
  502. struct drm_i915_gem_pwrite *args,
  503. struct drm_file *file)
  504. {
  505. drm_i915_private_t *dev_priv = dev->dev_private;
  506. ssize_t remain;
  507. loff_t offset, page_base;
  508. char __user *user_data;
  509. int page_offset, page_length, ret;
  510. ret = i915_gem_obj_ggtt_pin(obj, 0, true, true);
  511. if (ret)
  512. goto out;
  513. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  514. if (ret)
  515. goto out_unpin;
  516. ret = i915_gem_object_put_fence(obj);
  517. if (ret)
  518. goto out_unpin;
  519. user_data = to_user_ptr(args->data_ptr);
  520. remain = args->size;
  521. offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
  522. while (remain > 0) {
  523. /* Operation in this page
  524. *
  525. * page_base = page offset within aperture
  526. * page_offset = offset within page
  527. * page_length = bytes to copy for this page
  528. */
  529. page_base = offset & PAGE_MASK;
  530. page_offset = offset_in_page(offset);
  531. page_length = remain;
  532. if ((page_offset + remain) > PAGE_SIZE)
  533. page_length = PAGE_SIZE - page_offset;
  534. /* If we get a fault while copying data, then (presumably) our
  535. * source page isn't available. Return the error and we'll
  536. * retry in the slow path.
  537. */
  538. if (fast_user_write(dev_priv->gtt.mappable, page_base,
  539. page_offset, user_data, page_length)) {
  540. ret = -EFAULT;
  541. goto out_unpin;
  542. }
  543. remain -= page_length;
  544. user_data += page_length;
  545. offset += page_length;
  546. }
  547. out_unpin:
  548. i915_gem_object_unpin(obj);
  549. out:
  550. return ret;
  551. }
  552. /* Per-page copy function for the shmem pwrite fastpath.
  553. * Flushes invalid cachelines before writing to the target if
  554. * needs_clflush_before is set and flushes out any written cachelines after
  555. * writing if needs_clflush is set. */
  556. static int
  557. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  558. char __user *user_data,
  559. bool page_do_bit17_swizzling,
  560. bool needs_clflush_before,
  561. bool needs_clflush_after)
  562. {
  563. char *vaddr;
  564. int ret;
  565. if (unlikely(page_do_bit17_swizzling))
  566. return -EINVAL;
  567. vaddr = kmap_atomic(page);
  568. if (needs_clflush_before)
  569. drm_clflush_virt_range(vaddr + shmem_page_offset,
  570. page_length);
  571. ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
  572. user_data,
  573. page_length);
  574. if (needs_clflush_after)
  575. drm_clflush_virt_range(vaddr + shmem_page_offset,
  576. page_length);
  577. kunmap_atomic(vaddr);
  578. return ret ? -EFAULT : 0;
  579. }
  580. /* Only difference to the fast-path function is that this can handle bit17
  581. * and uses non-atomic copy and kmap functions. */
  582. static int
  583. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  584. char __user *user_data,
  585. bool page_do_bit17_swizzling,
  586. bool needs_clflush_before,
  587. bool needs_clflush_after)
  588. {
  589. char *vaddr;
  590. int ret;
  591. vaddr = kmap(page);
  592. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  593. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  594. page_length,
  595. page_do_bit17_swizzling);
  596. if (page_do_bit17_swizzling)
  597. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  598. user_data,
  599. page_length);
  600. else
  601. ret = __copy_from_user(vaddr + shmem_page_offset,
  602. user_data,
  603. page_length);
  604. if (needs_clflush_after)
  605. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  606. page_length,
  607. page_do_bit17_swizzling);
  608. kunmap(page);
  609. return ret ? -EFAULT : 0;
  610. }
  611. static int
  612. i915_gem_shmem_pwrite(struct drm_device *dev,
  613. struct drm_i915_gem_object *obj,
  614. struct drm_i915_gem_pwrite *args,
  615. struct drm_file *file)
  616. {
  617. ssize_t remain;
  618. loff_t offset;
  619. char __user *user_data;
  620. int shmem_page_offset, page_length, ret = 0;
  621. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  622. int hit_slowpath = 0;
  623. int needs_clflush_after = 0;
  624. int needs_clflush_before = 0;
  625. struct sg_page_iter sg_iter;
  626. user_data = to_user_ptr(args->data_ptr);
  627. remain = args->size;
  628. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  629. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  630. /* If we're not in the cpu write domain, set ourself into the gtt
  631. * write domain and manually flush cachelines (if required). This
  632. * optimizes for the case when the gpu will use the data
  633. * right away and we therefore have to clflush anyway. */
  634. needs_clflush_after = cpu_write_needs_clflush(obj);
  635. ret = i915_gem_object_wait_rendering(obj, false);
  636. if (ret)
  637. return ret;
  638. }
  639. /* Same trick applies to invalidate partially written cachelines read
  640. * before writing. */
  641. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  642. needs_clflush_before =
  643. !cpu_cache_is_coherent(dev, obj->cache_level);
  644. ret = i915_gem_object_get_pages(obj);
  645. if (ret)
  646. return ret;
  647. i915_gem_object_pin_pages(obj);
  648. offset = args->offset;
  649. obj->dirty = 1;
  650. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  651. offset >> PAGE_SHIFT) {
  652. struct page *page = sg_page_iter_page(&sg_iter);
  653. int partial_cacheline_write;
  654. if (remain <= 0)
  655. break;
  656. /* Operation in this page
  657. *
  658. * shmem_page_offset = offset within page in shmem file
  659. * page_length = bytes to copy for this page
  660. */
  661. shmem_page_offset = offset_in_page(offset);
  662. page_length = remain;
  663. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  664. page_length = PAGE_SIZE - shmem_page_offset;
  665. /* If we don't overwrite a cacheline completely we need to be
  666. * careful to have up-to-date data by first clflushing. Don't
  667. * overcomplicate things and flush the entire patch. */
  668. partial_cacheline_write = needs_clflush_before &&
  669. ((shmem_page_offset | page_length)
  670. & (boot_cpu_data.x86_clflush_size - 1));
  671. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  672. (page_to_phys(page) & (1 << 17)) != 0;
  673. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  674. user_data, page_do_bit17_swizzling,
  675. partial_cacheline_write,
  676. needs_clflush_after);
  677. if (ret == 0)
  678. goto next_page;
  679. hit_slowpath = 1;
  680. mutex_unlock(&dev->struct_mutex);
  681. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  682. user_data, page_do_bit17_swizzling,
  683. partial_cacheline_write,
  684. needs_clflush_after);
  685. mutex_lock(&dev->struct_mutex);
  686. next_page:
  687. set_page_dirty(page);
  688. mark_page_accessed(page);
  689. if (ret)
  690. goto out;
  691. remain -= page_length;
  692. user_data += page_length;
  693. offset += page_length;
  694. }
  695. out:
  696. i915_gem_object_unpin_pages(obj);
  697. if (hit_slowpath) {
  698. /*
  699. * Fixup: Flush cpu caches in case we didn't flush the dirty
  700. * cachelines in-line while writing and the object moved
  701. * out of the cpu write domain while we've dropped the lock.
  702. */
  703. if (!needs_clflush_after &&
  704. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  705. if (i915_gem_clflush_object(obj, obj->pin_display))
  706. i915_gem_chipset_flush(dev);
  707. }
  708. }
  709. if (needs_clflush_after)
  710. i915_gem_chipset_flush(dev);
  711. return ret;
  712. }
  713. /**
  714. * Writes data to the object referenced by handle.
  715. *
  716. * On error, the contents of the buffer that were to be modified are undefined.
  717. */
  718. int
  719. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  720. struct drm_file *file)
  721. {
  722. struct drm_i915_gem_pwrite *args = data;
  723. struct drm_i915_gem_object *obj;
  724. int ret;
  725. if (args->size == 0)
  726. return 0;
  727. if (!access_ok(VERIFY_READ,
  728. to_user_ptr(args->data_ptr),
  729. args->size))
  730. return -EFAULT;
  731. if (likely(!i915_prefault_disable)) {
  732. ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
  733. args->size);
  734. if (ret)
  735. return -EFAULT;
  736. }
  737. ret = i915_mutex_lock_interruptible(dev);
  738. if (ret)
  739. return ret;
  740. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  741. if (&obj->base == NULL) {
  742. ret = -ENOENT;
  743. goto unlock;
  744. }
  745. /* Bounds check destination. */
  746. if (args->offset > obj->base.size ||
  747. args->size > obj->base.size - args->offset) {
  748. ret = -EINVAL;
  749. goto out;
  750. }
  751. /* prime objects have no backing filp to GEM pread/pwrite
  752. * pages from.
  753. */
  754. if (!obj->base.filp) {
  755. ret = -EINVAL;
  756. goto out;
  757. }
  758. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  759. ret = -EFAULT;
  760. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  761. * it would end up going through the fenced access, and we'll get
  762. * different detiling behavior between reading and writing.
  763. * pread/pwrite currently are reading and writing from the CPU
  764. * perspective, requiring manual detiling by the client.
  765. */
  766. if (obj->phys_obj) {
  767. ret = i915_gem_phys_pwrite(dev, obj, args, file);
  768. goto out;
  769. }
  770. if (obj->tiling_mode == I915_TILING_NONE &&
  771. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  772. cpu_write_needs_clflush(obj)) {
  773. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  774. /* Note that the gtt paths might fail with non-page-backed user
  775. * pointers (e.g. gtt mappings when moving data between
  776. * textures). Fallback to the shmem path in that case. */
  777. }
  778. if (ret == -EFAULT || ret == -ENOSPC)
  779. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  780. out:
  781. drm_gem_object_unreference(&obj->base);
  782. unlock:
  783. mutex_unlock(&dev->struct_mutex);
  784. return ret;
  785. }
  786. int
  787. i915_gem_check_wedge(struct i915_gpu_error *error,
  788. bool interruptible)
  789. {
  790. if (i915_reset_in_progress(error)) {
  791. /* Non-interruptible callers can't handle -EAGAIN, hence return
  792. * -EIO unconditionally for these. */
  793. if (!interruptible)
  794. return -EIO;
  795. /* Recovery complete, but the reset failed ... */
  796. if (i915_terminally_wedged(error))
  797. return -EIO;
  798. return -EAGAIN;
  799. }
  800. return 0;
  801. }
  802. /*
  803. * Compare seqno against outstanding lazy request. Emit a request if they are
  804. * equal.
  805. */
  806. static int
  807. i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
  808. {
  809. int ret;
  810. BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  811. ret = 0;
  812. if (seqno == ring->outstanding_lazy_seqno)
  813. ret = i915_add_request(ring, NULL);
  814. return ret;
  815. }
  816. /**
  817. * __wait_seqno - wait until execution of seqno has finished
  818. * @ring: the ring expected to report seqno
  819. * @seqno: duh!
  820. * @reset_counter: reset sequence associated with the given seqno
  821. * @interruptible: do an interruptible wait (normally yes)
  822. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  823. *
  824. * Note: It is of utmost importance that the passed in seqno and reset_counter
  825. * values have been read by the caller in an smp safe manner. Where read-side
  826. * locks are involved, it is sufficient to read the reset_counter before
  827. * unlocking the lock that protects the seqno. For lockless tricks, the
  828. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  829. * inserted.
  830. *
  831. * Returns 0 if the seqno was found within the alloted time. Else returns the
  832. * errno with remaining time filled in timeout argument.
  833. */
  834. static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
  835. unsigned reset_counter,
  836. bool interruptible, struct timespec *timeout)
  837. {
  838. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  839. struct timespec before, now, wait_time={1,0};
  840. unsigned long timeout_jiffies;
  841. long end;
  842. bool wait_forever = true;
  843. int ret;
  844. WARN(dev_priv->pc8.irqs_disabled, "IRQs disabled\n");
  845. if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
  846. return 0;
  847. trace_i915_gem_request_wait_begin(ring, seqno);
  848. if (timeout != NULL) {
  849. wait_time = *timeout;
  850. wait_forever = false;
  851. }
  852. timeout_jiffies = timespec_to_jiffies_timeout(&wait_time);
  853. if (WARN_ON(!ring->irq_get(ring)))
  854. return -ENODEV;
  855. /* Record current time in case interrupted by signal, or wedged * */
  856. getrawmonotonic(&before);
  857. #define EXIT_COND \
  858. (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
  859. i915_reset_in_progress(&dev_priv->gpu_error) || \
  860. reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  861. do {
  862. if (interruptible)
  863. end = wait_event_interruptible_timeout(ring->irq_queue,
  864. EXIT_COND,
  865. timeout_jiffies);
  866. else
  867. end = wait_event_timeout(ring->irq_queue, EXIT_COND,
  868. timeout_jiffies);
  869. /* We need to check whether any gpu reset happened in between
  870. * the caller grabbing the seqno and now ... */
  871. if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
  872. end = -EAGAIN;
  873. /* ... but upgrade the -EGAIN to an -EIO if the gpu is truely
  874. * gone. */
  875. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  876. if (ret)
  877. end = ret;
  878. } while (end == 0 && wait_forever);
  879. getrawmonotonic(&now);
  880. ring->irq_put(ring);
  881. trace_i915_gem_request_wait_end(ring, seqno);
  882. #undef EXIT_COND
  883. if (timeout) {
  884. struct timespec sleep_time = timespec_sub(now, before);
  885. *timeout = timespec_sub(*timeout, sleep_time);
  886. if (!timespec_valid(timeout)) /* i.e. negative time remains */
  887. set_normalized_timespec(timeout, 0, 0);
  888. }
  889. switch (end) {
  890. case -EIO:
  891. case -EAGAIN: /* Wedged */
  892. case -ERESTARTSYS: /* Signal */
  893. return (int)end;
  894. case 0: /* Timeout */
  895. return -ETIME;
  896. default: /* Completed */
  897. WARN_ON(end < 0); /* We're not aware of other errors */
  898. return 0;
  899. }
  900. }
  901. /**
  902. * Waits for a sequence number to be signaled, and cleans up the
  903. * request and object lists appropriately for that event.
  904. */
  905. int
  906. i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
  907. {
  908. struct drm_device *dev = ring->dev;
  909. struct drm_i915_private *dev_priv = dev->dev_private;
  910. bool interruptible = dev_priv->mm.interruptible;
  911. int ret;
  912. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  913. BUG_ON(seqno == 0);
  914. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  915. if (ret)
  916. return ret;
  917. ret = i915_gem_check_olr(ring, seqno);
  918. if (ret)
  919. return ret;
  920. return __wait_seqno(ring, seqno,
  921. atomic_read(&dev_priv->gpu_error.reset_counter),
  922. interruptible, NULL);
  923. }
  924. static int
  925. i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj,
  926. struct intel_ring_buffer *ring)
  927. {
  928. i915_gem_retire_requests_ring(ring);
  929. /* Manually manage the write flush as we may have not yet
  930. * retired the buffer.
  931. *
  932. * Note that the last_write_seqno is always the earlier of
  933. * the two (read/write) seqno, so if we haved successfully waited,
  934. * we know we have passed the last write.
  935. */
  936. obj->last_write_seqno = 0;
  937. obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  938. return 0;
  939. }
  940. /**
  941. * Ensures that all rendering to the object has completed and the object is
  942. * safe to unbind from the GTT or access from the CPU.
  943. */
  944. static __must_check int
  945. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  946. bool readonly)
  947. {
  948. struct intel_ring_buffer *ring = obj->ring;
  949. u32 seqno;
  950. int ret;
  951. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  952. if (seqno == 0)
  953. return 0;
  954. ret = i915_wait_seqno(ring, seqno);
  955. if (ret)
  956. return ret;
  957. return i915_gem_object_wait_rendering__tail(obj, ring);
  958. }
  959. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  960. * as the object state may change during this call.
  961. */
  962. static __must_check int
  963. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  964. bool readonly)
  965. {
  966. struct drm_device *dev = obj->base.dev;
  967. struct drm_i915_private *dev_priv = dev->dev_private;
  968. struct intel_ring_buffer *ring = obj->ring;
  969. unsigned reset_counter;
  970. u32 seqno;
  971. int ret;
  972. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  973. BUG_ON(!dev_priv->mm.interruptible);
  974. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  975. if (seqno == 0)
  976. return 0;
  977. ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
  978. if (ret)
  979. return ret;
  980. ret = i915_gem_check_olr(ring, seqno);
  981. if (ret)
  982. return ret;
  983. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  984. mutex_unlock(&dev->struct_mutex);
  985. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
  986. mutex_lock(&dev->struct_mutex);
  987. if (ret)
  988. return ret;
  989. return i915_gem_object_wait_rendering__tail(obj, ring);
  990. }
  991. /**
  992. * Called when user space prepares to use an object with the CPU, either
  993. * through the mmap ioctl's mapping or a GTT mapping.
  994. */
  995. int
  996. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  997. struct drm_file *file)
  998. {
  999. struct drm_i915_gem_set_domain *args = data;
  1000. struct drm_i915_gem_object *obj;
  1001. uint32_t read_domains = args->read_domains;
  1002. uint32_t write_domain = args->write_domain;
  1003. int ret;
  1004. /* Only handle setting domains to types used by the CPU. */
  1005. if (write_domain & I915_GEM_GPU_DOMAINS)
  1006. return -EINVAL;
  1007. if (read_domains & I915_GEM_GPU_DOMAINS)
  1008. return -EINVAL;
  1009. /* Having something in the write domain implies it's in the read
  1010. * domain, and only that read domain. Enforce that in the request.
  1011. */
  1012. if (write_domain != 0 && read_domains != write_domain)
  1013. return -EINVAL;
  1014. ret = i915_mutex_lock_interruptible(dev);
  1015. if (ret)
  1016. return ret;
  1017. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1018. if (&obj->base == NULL) {
  1019. ret = -ENOENT;
  1020. goto unlock;
  1021. }
  1022. /* Try to flush the object off the GPU without holding the lock.
  1023. * We will repeat the flush holding the lock in the normal manner
  1024. * to catch cases where we are gazumped.
  1025. */
  1026. ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain);
  1027. if (ret)
  1028. goto unref;
  1029. if (read_domains & I915_GEM_DOMAIN_GTT) {
  1030. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1031. /* Silently promote "you're not bound, there was nothing to do"
  1032. * to success, since the client was just asking us to
  1033. * make sure everything was done.
  1034. */
  1035. if (ret == -EINVAL)
  1036. ret = 0;
  1037. } else {
  1038. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1039. }
  1040. unref:
  1041. drm_gem_object_unreference(&obj->base);
  1042. unlock:
  1043. mutex_unlock(&dev->struct_mutex);
  1044. return ret;
  1045. }
  1046. /**
  1047. * Called when user space has done writes to this buffer
  1048. */
  1049. int
  1050. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1051. struct drm_file *file)
  1052. {
  1053. struct drm_i915_gem_sw_finish *args = data;
  1054. struct drm_i915_gem_object *obj;
  1055. int ret = 0;
  1056. ret = i915_mutex_lock_interruptible(dev);
  1057. if (ret)
  1058. return ret;
  1059. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1060. if (&obj->base == NULL) {
  1061. ret = -ENOENT;
  1062. goto unlock;
  1063. }
  1064. /* Pinned buffers may be scanout, so flush the cache */
  1065. if (obj->pin_display)
  1066. i915_gem_object_flush_cpu_write_domain(obj, true);
  1067. drm_gem_object_unreference(&obj->base);
  1068. unlock:
  1069. mutex_unlock(&dev->struct_mutex);
  1070. return ret;
  1071. }
  1072. /**
  1073. * Maps the contents of an object, returning the address it is mapped
  1074. * into.
  1075. *
  1076. * While the mapping holds a reference on the contents of the object, it doesn't
  1077. * imply a ref on the object itself.
  1078. */
  1079. int
  1080. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1081. struct drm_file *file)
  1082. {
  1083. struct drm_i915_gem_mmap *args = data;
  1084. struct drm_gem_object *obj;
  1085. unsigned long addr;
  1086. obj = drm_gem_object_lookup(dev, file, args->handle);
  1087. if (obj == NULL)
  1088. return -ENOENT;
  1089. /* prime objects have no backing filp to GEM mmap
  1090. * pages from.
  1091. */
  1092. if (!obj->filp) {
  1093. drm_gem_object_unreference_unlocked(obj);
  1094. return -EINVAL;
  1095. }
  1096. addr = vm_mmap(obj->filp, 0, args->size,
  1097. PROT_READ | PROT_WRITE, MAP_SHARED,
  1098. args->offset);
  1099. drm_gem_object_unreference_unlocked(obj);
  1100. if (IS_ERR((void *)addr))
  1101. return addr;
  1102. args->addr_ptr = (uint64_t) addr;
  1103. return 0;
  1104. }
  1105. /**
  1106. * i915_gem_fault - fault a page into the GTT
  1107. * vma: VMA in question
  1108. * vmf: fault info
  1109. *
  1110. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1111. * from userspace. The fault handler takes care of binding the object to
  1112. * the GTT (if needed), allocating and programming a fence register (again,
  1113. * only if needed based on whether the old reg is still valid or the object
  1114. * is tiled) and inserting a new PTE into the faulting process.
  1115. *
  1116. * Note that the faulting process may involve evicting existing objects
  1117. * from the GTT and/or fence registers to make room. So performance may
  1118. * suffer if the GTT working set is large or there are few fence registers
  1119. * left.
  1120. */
  1121. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1122. {
  1123. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1124. struct drm_device *dev = obj->base.dev;
  1125. drm_i915_private_t *dev_priv = dev->dev_private;
  1126. pgoff_t page_offset;
  1127. unsigned long pfn;
  1128. int ret = 0;
  1129. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1130. /* We don't use vmf->pgoff since that has the fake offset */
  1131. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1132. PAGE_SHIFT;
  1133. ret = i915_mutex_lock_interruptible(dev);
  1134. if (ret)
  1135. goto out;
  1136. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1137. /* Access to snoopable pages through the GTT is incoherent. */
  1138. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1139. ret = -EINVAL;
  1140. goto unlock;
  1141. }
  1142. /* Now bind it into the GTT if needed */
  1143. ret = i915_gem_obj_ggtt_pin(obj, 0, true, false);
  1144. if (ret)
  1145. goto unlock;
  1146. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1147. if (ret)
  1148. goto unpin;
  1149. ret = i915_gem_object_get_fence(obj);
  1150. if (ret)
  1151. goto unpin;
  1152. obj->fault_mappable = true;
  1153. pfn = dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj);
  1154. pfn >>= PAGE_SHIFT;
  1155. pfn += page_offset;
  1156. /* Finally, remap it using the new GTT offset */
  1157. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  1158. unpin:
  1159. i915_gem_object_unpin(obj);
  1160. unlock:
  1161. mutex_unlock(&dev->struct_mutex);
  1162. out:
  1163. switch (ret) {
  1164. case -EIO:
  1165. /* If this -EIO is due to a gpu hang, give the reset code a
  1166. * chance to clean up the mess. Otherwise return the proper
  1167. * SIGBUS. */
  1168. if (i915_terminally_wedged(&dev_priv->gpu_error))
  1169. return VM_FAULT_SIGBUS;
  1170. case -EAGAIN:
  1171. /*
  1172. * EAGAIN means the gpu is hung and we'll wait for the error
  1173. * handler to reset everything when re-faulting in
  1174. * i915_mutex_lock_interruptible.
  1175. */
  1176. case 0:
  1177. case -ERESTARTSYS:
  1178. case -EINTR:
  1179. case -EBUSY:
  1180. /*
  1181. * EBUSY is ok: this just means that another thread
  1182. * already did the job.
  1183. */
  1184. return VM_FAULT_NOPAGE;
  1185. case -ENOMEM:
  1186. return VM_FAULT_OOM;
  1187. case -ENOSPC:
  1188. return VM_FAULT_SIGBUS;
  1189. default:
  1190. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1191. return VM_FAULT_SIGBUS;
  1192. }
  1193. }
  1194. /**
  1195. * i915_gem_release_mmap - remove physical page mappings
  1196. * @obj: obj in question
  1197. *
  1198. * Preserve the reservation of the mmapping with the DRM core code, but
  1199. * relinquish ownership of the pages back to the system.
  1200. *
  1201. * It is vital that we remove the page mapping if we have mapped a tiled
  1202. * object through the GTT and then lose the fence register due to
  1203. * resource pressure. Similarly if the object has been moved out of the
  1204. * aperture, than pages mapped into userspace must be revoked. Removing the
  1205. * mapping will then trigger a page fault on the next user access, allowing
  1206. * fixup by i915_gem_fault().
  1207. */
  1208. void
  1209. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1210. {
  1211. if (!obj->fault_mappable)
  1212. return;
  1213. drm_vma_node_unmap(&obj->base.vma_node, obj->base.dev->dev_mapping);
  1214. obj->fault_mappable = false;
  1215. }
  1216. uint32_t
  1217. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1218. {
  1219. uint32_t gtt_size;
  1220. if (INTEL_INFO(dev)->gen >= 4 ||
  1221. tiling_mode == I915_TILING_NONE)
  1222. return size;
  1223. /* Previous chips need a power-of-two fence region when tiling */
  1224. if (INTEL_INFO(dev)->gen == 3)
  1225. gtt_size = 1024*1024;
  1226. else
  1227. gtt_size = 512*1024;
  1228. while (gtt_size < size)
  1229. gtt_size <<= 1;
  1230. return gtt_size;
  1231. }
  1232. /**
  1233. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1234. * @obj: object to check
  1235. *
  1236. * Return the required GTT alignment for an object, taking into account
  1237. * potential fence register mapping.
  1238. */
  1239. uint32_t
  1240. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1241. int tiling_mode, bool fenced)
  1242. {
  1243. /*
  1244. * Minimum alignment is 4k (GTT page size), but might be greater
  1245. * if a fence register is needed for the object.
  1246. */
  1247. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1248. tiling_mode == I915_TILING_NONE)
  1249. return 4096;
  1250. /*
  1251. * Previous chips need to be aligned to the size of the smallest
  1252. * fence register that can contain the object.
  1253. */
  1254. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1255. }
  1256. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1257. {
  1258. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1259. int ret;
  1260. if (drm_vma_node_has_offset(&obj->base.vma_node))
  1261. return 0;
  1262. dev_priv->mm.shrinker_no_lock_stealing = true;
  1263. ret = drm_gem_create_mmap_offset(&obj->base);
  1264. if (ret != -ENOSPC)
  1265. goto out;
  1266. /* Badly fragmented mmap space? The only way we can recover
  1267. * space is by destroying unwanted objects. We can't randomly release
  1268. * mmap_offsets as userspace expects them to be persistent for the
  1269. * lifetime of the objects. The closest we can is to release the
  1270. * offsets on purgeable objects by truncating it and marking it purged,
  1271. * which prevents userspace from ever using that object again.
  1272. */
  1273. i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
  1274. ret = drm_gem_create_mmap_offset(&obj->base);
  1275. if (ret != -ENOSPC)
  1276. goto out;
  1277. i915_gem_shrink_all(dev_priv);
  1278. ret = drm_gem_create_mmap_offset(&obj->base);
  1279. out:
  1280. dev_priv->mm.shrinker_no_lock_stealing = false;
  1281. return ret;
  1282. }
  1283. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1284. {
  1285. drm_gem_free_mmap_offset(&obj->base);
  1286. }
  1287. int
  1288. i915_gem_mmap_gtt(struct drm_file *file,
  1289. struct drm_device *dev,
  1290. uint32_t handle,
  1291. uint64_t *offset)
  1292. {
  1293. struct drm_i915_private *dev_priv = dev->dev_private;
  1294. struct drm_i915_gem_object *obj;
  1295. int ret;
  1296. ret = i915_mutex_lock_interruptible(dev);
  1297. if (ret)
  1298. return ret;
  1299. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1300. if (&obj->base == NULL) {
  1301. ret = -ENOENT;
  1302. goto unlock;
  1303. }
  1304. if (obj->base.size > dev_priv->gtt.mappable_end) {
  1305. ret = -E2BIG;
  1306. goto out;
  1307. }
  1308. if (obj->madv != I915_MADV_WILLNEED) {
  1309. DRM_ERROR("Attempting to mmap a purgeable buffer\n");
  1310. ret = -EINVAL;
  1311. goto out;
  1312. }
  1313. ret = i915_gem_object_create_mmap_offset(obj);
  1314. if (ret)
  1315. goto out;
  1316. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1317. out:
  1318. drm_gem_object_unreference(&obj->base);
  1319. unlock:
  1320. mutex_unlock(&dev->struct_mutex);
  1321. return ret;
  1322. }
  1323. /**
  1324. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1325. * @dev: DRM device
  1326. * @data: GTT mapping ioctl data
  1327. * @file: GEM object info
  1328. *
  1329. * Simply returns the fake offset to userspace so it can mmap it.
  1330. * The mmap call will end up in drm_gem_mmap(), which will set things
  1331. * up so we can get faults in the handler above.
  1332. *
  1333. * The fault handler will take care of binding the object into the GTT
  1334. * (since it may have been evicted to make room for something), allocating
  1335. * a fence register, and mapping the appropriate aperture address into
  1336. * userspace.
  1337. */
  1338. int
  1339. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1340. struct drm_file *file)
  1341. {
  1342. struct drm_i915_gem_mmap_gtt *args = data;
  1343. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1344. }
  1345. /* Immediately discard the backing storage */
  1346. static void
  1347. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1348. {
  1349. struct inode *inode;
  1350. i915_gem_object_free_mmap_offset(obj);
  1351. if (obj->base.filp == NULL)
  1352. return;
  1353. /* Our goal here is to return as much of the memory as
  1354. * is possible back to the system as we are called from OOM.
  1355. * To do this we must instruct the shmfs to drop all of its
  1356. * backing pages, *now*.
  1357. */
  1358. inode = file_inode(obj->base.filp);
  1359. shmem_truncate_range(inode, 0, (loff_t)-1);
  1360. obj->madv = __I915_MADV_PURGED;
  1361. }
  1362. static inline int
  1363. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1364. {
  1365. return obj->madv == I915_MADV_DONTNEED;
  1366. }
  1367. static void
  1368. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1369. {
  1370. struct sg_page_iter sg_iter;
  1371. int ret;
  1372. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1373. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1374. if (ret) {
  1375. /* In the event of a disaster, abandon all caches and
  1376. * hope for the best.
  1377. */
  1378. WARN_ON(ret != -EIO);
  1379. i915_gem_clflush_object(obj, true);
  1380. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1381. }
  1382. if (i915_gem_object_needs_bit17_swizzle(obj))
  1383. i915_gem_object_save_bit_17_swizzle(obj);
  1384. if (obj->madv == I915_MADV_DONTNEED)
  1385. obj->dirty = 0;
  1386. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  1387. struct page *page = sg_page_iter_page(&sg_iter);
  1388. if (obj->dirty)
  1389. set_page_dirty(page);
  1390. if (obj->madv == I915_MADV_WILLNEED)
  1391. mark_page_accessed(page);
  1392. page_cache_release(page);
  1393. }
  1394. obj->dirty = 0;
  1395. sg_free_table(obj->pages);
  1396. kfree(obj->pages);
  1397. }
  1398. int
  1399. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1400. {
  1401. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1402. if (obj->pages == NULL)
  1403. return 0;
  1404. if (obj->pages_pin_count)
  1405. return -EBUSY;
  1406. BUG_ON(i915_gem_obj_bound_any(obj));
  1407. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1408. * array, hence protect them from being reaped by removing them from gtt
  1409. * lists early. */
  1410. list_del(&obj->global_list);
  1411. ops->put_pages(obj);
  1412. obj->pages = NULL;
  1413. if (i915_gem_object_is_purgeable(obj))
  1414. i915_gem_object_truncate(obj);
  1415. return 0;
  1416. }
  1417. static long
  1418. __i915_gem_shrink(struct drm_i915_private *dev_priv, long target,
  1419. bool purgeable_only)
  1420. {
  1421. struct list_head still_bound_list;
  1422. struct drm_i915_gem_object *obj, *next;
  1423. long count = 0;
  1424. list_for_each_entry_safe(obj, next,
  1425. &dev_priv->mm.unbound_list,
  1426. global_list) {
  1427. if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
  1428. i915_gem_object_put_pages(obj) == 0) {
  1429. count += obj->base.size >> PAGE_SHIFT;
  1430. if (count >= target)
  1431. return count;
  1432. }
  1433. }
  1434. /*
  1435. * As we may completely rewrite the bound list whilst unbinding
  1436. * (due to retiring requests) we have to strictly process only
  1437. * one element of the list at the time, and recheck the list
  1438. * on every iteration.
  1439. */
  1440. INIT_LIST_HEAD(&still_bound_list);
  1441. while (count < target && !list_empty(&dev_priv->mm.bound_list)) {
  1442. struct i915_vma *vma, *v;
  1443. obj = list_first_entry(&dev_priv->mm.bound_list,
  1444. typeof(*obj), global_list);
  1445. list_move_tail(&obj->global_list, &still_bound_list);
  1446. if (!i915_gem_object_is_purgeable(obj) && purgeable_only)
  1447. continue;
  1448. /*
  1449. * Hold a reference whilst we unbind this object, as we may
  1450. * end up waiting for and retiring requests. This might
  1451. * release the final reference (held by the active list)
  1452. * and result in the object being freed from under us.
  1453. * in this object being freed.
  1454. *
  1455. * Note 1: Shrinking the bound list is special since only active
  1456. * (and hence bound objects) can contain such limbo objects, so
  1457. * we don't need special tricks for shrinking the unbound list.
  1458. * The only other place where we have to be careful with active
  1459. * objects suddenly disappearing due to retiring requests is the
  1460. * eviction code.
  1461. *
  1462. * Note 2: Even though the bound list doesn't hold a reference
  1463. * to the object we can safely grab one here: The final object
  1464. * unreferencing and the bound_list are both protected by the
  1465. * dev->struct_mutex and so we won't ever be able to observe an
  1466. * object on the bound_list with a reference count equals 0.
  1467. */
  1468. drm_gem_object_reference(&obj->base);
  1469. list_for_each_entry_safe(vma, v, &obj->vma_list, vma_link)
  1470. if (i915_vma_unbind(vma))
  1471. break;
  1472. if (i915_gem_object_put_pages(obj) == 0)
  1473. count += obj->base.size >> PAGE_SHIFT;
  1474. drm_gem_object_unreference(&obj->base);
  1475. }
  1476. list_splice(&still_bound_list, &dev_priv->mm.bound_list);
  1477. return count;
  1478. }
  1479. static long
  1480. i915_gem_purge(struct drm_i915_private *dev_priv, long target)
  1481. {
  1482. return __i915_gem_shrink(dev_priv, target, true);
  1483. }
  1484. static long
  1485. i915_gem_shrink_all(struct drm_i915_private *dev_priv)
  1486. {
  1487. struct drm_i915_gem_object *obj, *next;
  1488. long freed = 0;
  1489. i915_gem_evict_everything(dev_priv->dev);
  1490. list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list,
  1491. global_list) {
  1492. if (obj->pages_pin_count == 0)
  1493. freed += obj->base.size >> PAGE_SHIFT;
  1494. i915_gem_object_put_pages(obj);
  1495. }
  1496. return freed;
  1497. }
  1498. static int
  1499. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1500. {
  1501. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1502. int page_count, i;
  1503. struct address_space *mapping;
  1504. struct sg_table *st;
  1505. struct scatterlist *sg;
  1506. struct sg_page_iter sg_iter;
  1507. struct page *page;
  1508. unsigned long last_pfn = 0; /* suppress gcc warning */
  1509. gfp_t gfp;
  1510. /* Assert that the object is not currently in any GPU domain. As it
  1511. * wasn't in the GTT, there shouldn't be any way it could have been in
  1512. * a GPU cache
  1513. */
  1514. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1515. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1516. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1517. if (st == NULL)
  1518. return -ENOMEM;
  1519. page_count = obj->base.size / PAGE_SIZE;
  1520. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1521. kfree(st);
  1522. return -ENOMEM;
  1523. }
  1524. /* Get the list of pages out of our struct file. They'll be pinned
  1525. * at this point until we release them.
  1526. *
  1527. * Fail silently without starting the shrinker
  1528. */
  1529. mapping = file_inode(obj->base.filp)->i_mapping;
  1530. gfp = mapping_gfp_mask(mapping);
  1531. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1532. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1533. sg = st->sgl;
  1534. st->nents = 0;
  1535. for (i = 0; i < page_count; i++) {
  1536. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1537. if (IS_ERR(page)) {
  1538. i915_gem_purge(dev_priv, page_count);
  1539. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1540. }
  1541. if (IS_ERR(page)) {
  1542. /* We've tried hard to allocate the memory by reaping
  1543. * our own buffer, now let the real VM do its job and
  1544. * go down in flames if truly OOM.
  1545. */
  1546. gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
  1547. gfp |= __GFP_IO | __GFP_WAIT;
  1548. i915_gem_shrink_all(dev_priv);
  1549. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1550. if (IS_ERR(page))
  1551. goto err_pages;
  1552. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1553. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1554. }
  1555. #ifdef CONFIG_SWIOTLB
  1556. if (swiotlb_nr_tbl()) {
  1557. st->nents++;
  1558. sg_set_page(sg, page, PAGE_SIZE, 0);
  1559. sg = sg_next(sg);
  1560. continue;
  1561. }
  1562. #endif
  1563. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1564. if (i)
  1565. sg = sg_next(sg);
  1566. st->nents++;
  1567. sg_set_page(sg, page, PAGE_SIZE, 0);
  1568. } else {
  1569. sg->length += PAGE_SIZE;
  1570. }
  1571. last_pfn = page_to_pfn(page);
  1572. }
  1573. #ifdef CONFIG_SWIOTLB
  1574. if (!swiotlb_nr_tbl())
  1575. #endif
  1576. sg_mark_end(sg);
  1577. obj->pages = st;
  1578. if (i915_gem_object_needs_bit17_swizzle(obj))
  1579. i915_gem_object_do_bit_17_swizzle(obj);
  1580. return 0;
  1581. err_pages:
  1582. sg_mark_end(sg);
  1583. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  1584. page_cache_release(sg_page_iter_page(&sg_iter));
  1585. sg_free_table(st);
  1586. kfree(st);
  1587. return PTR_ERR(page);
  1588. }
  1589. /* Ensure that the associated pages are gathered from the backing storage
  1590. * and pinned into our object. i915_gem_object_get_pages() may be called
  1591. * multiple times before they are released by a single call to
  1592. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1593. * either as a result of memory pressure (reaping pages under the shrinker)
  1594. * or as the object is itself released.
  1595. */
  1596. int
  1597. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1598. {
  1599. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1600. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1601. int ret;
  1602. if (obj->pages)
  1603. return 0;
  1604. if (obj->madv != I915_MADV_WILLNEED) {
  1605. DRM_ERROR("Attempting to obtain a purgeable object\n");
  1606. return -EINVAL;
  1607. }
  1608. BUG_ON(obj->pages_pin_count);
  1609. ret = ops->get_pages(obj);
  1610. if (ret)
  1611. return ret;
  1612. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1613. return 0;
  1614. }
  1615. void
  1616. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1617. struct intel_ring_buffer *ring)
  1618. {
  1619. struct drm_device *dev = obj->base.dev;
  1620. struct drm_i915_private *dev_priv = dev->dev_private;
  1621. u32 seqno = intel_ring_get_seqno(ring);
  1622. BUG_ON(ring == NULL);
  1623. if (obj->ring != ring && obj->last_write_seqno) {
  1624. /* Keep the seqno relative to the current ring */
  1625. obj->last_write_seqno = seqno;
  1626. }
  1627. obj->ring = ring;
  1628. /* Add a reference if we're newly entering the active list. */
  1629. if (!obj->active) {
  1630. drm_gem_object_reference(&obj->base);
  1631. obj->active = 1;
  1632. }
  1633. list_move_tail(&obj->ring_list, &ring->active_list);
  1634. obj->last_read_seqno = seqno;
  1635. if (obj->fenced_gpu_access) {
  1636. obj->last_fenced_seqno = seqno;
  1637. /* Bump MRU to take account of the delayed flush */
  1638. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  1639. struct drm_i915_fence_reg *reg;
  1640. reg = &dev_priv->fence_regs[obj->fence_reg];
  1641. list_move_tail(&reg->lru_list,
  1642. &dev_priv->mm.fence_list);
  1643. }
  1644. }
  1645. }
  1646. static void
  1647. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1648. {
  1649. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1650. struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
  1651. struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
  1652. BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
  1653. BUG_ON(!obj->active);
  1654. list_move_tail(&vma->mm_list, &ggtt_vm->inactive_list);
  1655. list_del_init(&obj->ring_list);
  1656. obj->ring = NULL;
  1657. obj->last_read_seqno = 0;
  1658. obj->last_write_seqno = 0;
  1659. obj->base.write_domain = 0;
  1660. obj->last_fenced_seqno = 0;
  1661. obj->fenced_gpu_access = false;
  1662. obj->active = 0;
  1663. drm_gem_object_unreference(&obj->base);
  1664. WARN_ON(i915_verify_lists(dev));
  1665. }
  1666. static int
  1667. i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
  1668. {
  1669. struct drm_i915_private *dev_priv = dev->dev_private;
  1670. struct intel_ring_buffer *ring;
  1671. int ret, i, j;
  1672. /* Carefully retire all requests without writing to the rings */
  1673. for_each_ring(ring, dev_priv, i) {
  1674. ret = intel_ring_idle(ring);
  1675. if (ret)
  1676. return ret;
  1677. }
  1678. i915_gem_retire_requests(dev);
  1679. /* Finally reset hw state */
  1680. for_each_ring(ring, dev_priv, i) {
  1681. intel_ring_init_seqno(ring, seqno);
  1682. for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
  1683. ring->sync_seqno[j] = 0;
  1684. }
  1685. return 0;
  1686. }
  1687. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  1688. {
  1689. struct drm_i915_private *dev_priv = dev->dev_private;
  1690. int ret;
  1691. if (seqno == 0)
  1692. return -EINVAL;
  1693. /* HWS page needs to be set less than what we
  1694. * will inject to ring
  1695. */
  1696. ret = i915_gem_init_seqno(dev, seqno - 1);
  1697. if (ret)
  1698. return ret;
  1699. /* Carefully set the last_seqno value so that wrap
  1700. * detection still works
  1701. */
  1702. dev_priv->next_seqno = seqno;
  1703. dev_priv->last_seqno = seqno - 1;
  1704. if (dev_priv->last_seqno == 0)
  1705. dev_priv->last_seqno--;
  1706. return 0;
  1707. }
  1708. int
  1709. i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
  1710. {
  1711. struct drm_i915_private *dev_priv = dev->dev_private;
  1712. /* reserve 0 for non-seqno */
  1713. if (dev_priv->next_seqno == 0) {
  1714. int ret = i915_gem_init_seqno(dev, 0);
  1715. if (ret)
  1716. return ret;
  1717. dev_priv->next_seqno = 1;
  1718. }
  1719. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  1720. return 0;
  1721. }
  1722. int __i915_add_request(struct intel_ring_buffer *ring,
  1723. struct drm_file *file,
  1724. struct drm_i915_gem_object *obj,
  1725. u32 *out_seqno)
  1726. {
  1727. drm_i915_private_t *dev_priv = ring->dev->dev_private;
  1728. struct drm_i915_gem_request *request;
  1729. u32 request_ring_position, request_start;
  1730. int was_empty;
  1731. int ret;
  1732. request_start = intel_ring_get_tail(ring);
  1733. /*
  1734. * Emit any outstanding flushes - execbuf can fail to emit the flush
  1735. * after having emitted the batchbuffer command. Hence we need to fix
  1736. * things up similar to emitting the lazy request. The difference here
  1737. * is that the flush _must_ happen before the next request, no matter
  1738. * what.
  1739. */
  1740. ret = intel_ring_flush_all_caches(ring);
  1741. if (ret)
  1742. return ret;
  1743. request = ring->preallocated_lazy_request;
  1744. if (WARN_ON(request == NULL))
  1745. return -ENOMEM;
  1746. /* Record the position of the start of the request so that
  1747. * should we detect the updated seqno part-way through the
  1748. * GPU processing the request, we never over-estimate the
  1749. * position of the head.
  1750. */
  1751. request_ring_position = intel_ring_get_tail(ring);
  1752. ret = ring->add_request(ring);
  1753. if (ret)
  1754. return ret;
  1755. request->seqno = intel_ring_get_seqno(ring);
  1756. request->ring = ring;
  1757. request->head = request_start;
  1758. request->tail = request_ring_position;
  1759. /* Whilst this request exists, batch_obj will be on the
  1760. * active_list, and so will hold the active reference. Only when this
  1761. * request is retired will the the batch_obj be moved onto the
  1762. * inactive_list and lose its active reference. Hence we do not need
  1763. * to explicitly hold another reference here.
  1764. */
  1765. request->batch_obj = obj;
  1766. /* Hold a reference to the current context so that we can inspect
  1767. * it later in case a hangcheck error event fires.
  1768. */
  1769. request->ctx = ring->last_context;
  1770. if (request->ctx)
  1771. i915_gem_context_reference(request->ctx);
  1772. request->emitted_jiffies = jiffies;
  1773. was_empty = list_empty(&ring->request_list);
  1774. list_add_tail(&request->list, &ring->request_list);
  1775. request->file_priv = NULL;
  1776. if (file) {
  1777. struct drm_i915_file_private *file_priv = file->driver_priv;
  1778. spin_lock(&file_priv->mm.lock);
  1779. request->file_priv = file_priv;
  1780. list_add_tail(&request->client_list,
  1781. &file_priv->mm.request_list);
  1782. spin_unlock(&file_priv->mm.lock);
  1783. }
  1784. trace_i915_gem_request_add(ring, request->seqno);
  1785. ring->outstanding_lazy_seqno = 0;
  1786. ring->preallocated_lazy_request = NULL;
  1787. if (!dev_priv->ums.mm_suspended) {
  1788. i915_queue_hangcheck(ring->dev);
  1789. if (was_empty) {
  1790. queue_delayed_work(dev_priv->wq,
  1791. &dev_priv->mm.retire_work,
  1792. round_jiffies_up_relative(HZ));
  1793. intel_mark_busy(dev_priv->dev);
  1794. }
  1795. }
  1796. if (out_seqno)
  1797. *out_seqno = request->seqno;
  1798. return 0;
  1799. }
  1800. static inline void
  1801. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1802. {
  1803. struct drm_i915_file_private *file_priv = request->file_priv;
  1804. if (!file_priv)
  1805. return;
  1806. spin_lock(&file_priv->mm.lock);
  1807. if (request->file_priv) {
  1808. list_del(&request->client_list);
  1809. request->file_priv = NULL;
  1810. }
  1811. spin_unlock(&file_priv->mm.lock);
  1812. }
  1813. static bool i915_head_inside_object(u32 acthd, struct drm_i915_gem_object *obj,
  1814. struct i915_address_space *vm)
  1815. {
  1816. if (acthd >= i915_gem_obj_offset(obj, vm) &&
  1817. acthd < i915_gem_obj_offset(obj, vm) + obj->base.size)
  1818. return true;
  1819. return false;
  1820. }
  1821. static bool i915_head_inside_request(const u32 acthd_unmasked,
  1822. const u32 request_start,
  1823. const u32 request_end)
  1824. {
  1825. const u32 acthd = acthd_unmasked & HEAD_ADDR;
  1826. if (request_start < request_end) {
  1827. if (acthd >= request_start && acthd < request_end)
  1828. return true;
  1829. } else if (request_start > request_end) {
  1830. if (acthd >= request_start || acthd < request_end)
  1831. return true;
  1832. }
  1833. return false;
  1834. }
  1835. static struct i915_address_space *
  1836. request_to_vm(struct drm_i915_gem_request *request)
  1837. {
  1838. struct drm_i915_private *dev_priv = request->ring->dev->dev_private;
  1839. struct i915_address_space *vm;
  1840. vm = &dev_priv->gtt.base;
  1841. return vm;
  1842. }
  1843. static bool i915_request_guilty(struct drm_i915_gem_request *request,
  1844. const u32 acthd, bool *inside)
  1845. {
  1846. /* There is a possibility that unmasked head address
  1847. * pointing inside the ring, matches the batch_obj address range.
  1848. * However this is extremely unlikely.
  1849. */
  1850. if (request->batch_obj) {
  1851. if (i915_head_inside_object(acthd, request->batch_obj,
  1852. request_to_vm(request))) {
  1853. *inside = true;
  1854. return true;
  1855. }
  1856. }
  1857. if (i915_head_inside_request(acthd, request->head, request->tail)) {
  1858. *inside = false;
  1859. return true;
  1860. }
  1861. return false;
  1862. }
  1863. static bool i915_context_is_banned(const struct i915_ctx_hang_stats *hs)
  1864. {
  1865. const unsigned long elapsed = get_seconds() - hs->guilty_ts;
  1866. if (hs->banned)
  1867. return true;
  1868. if (elapsed <= DRM_I915_CTX_BAN_PERIOD) {
  1869. DRM_ERROR("context hanging too fast, declaring banned!\n");
  1870. return true;
  1871. }
  1872. return false;
  1873. }
  1874. static void i915_set_reset_status(struct intel_ring_buffer *ring,
  1875. struct drm_i915_gem_request *request,
  1876. u32 acthd)
  1877. {
  1878. struct i915_ctx_hang_stats *hs = NULL;
  1879. bool inside, guilty;
  1880. unsigned long offset = 0;
  1881. /* Innocent until proven guilty */
  1882. guilty = false;
  1883. if (request->batch_obj)
  1884. offset = i915_gem_obj_offset(request->batch_obj,
  1885. request_to_vm(request));
  1886. if (ring->hangcheck.action != HANGCHECK_WAIT &&
  1887. i915_request_guilty(request, acthd, &inside)) {
  1888. DRM_ERROR("%s hung %s bo (0x%lx ctx %d) at 0x%x\n",
  1889. ring->name,
  1890. inside ? "inside" : "flushing",
  1891. offset,
  1892. request->ctx ? request->ctx->id : 0,
  1893. acthd);
  1894. guilty = true;
  1895. }
  1896. /* If contexts are disabled or this is the default context, use
  1897. * file_priv->reset_state
  1898. */
  1899. if (request->ctx && request->ctx->id != DEFAULT_CONTEXT_ID)
  1900. hs = &request->ctx->hang_stats;
  1901. else if (request->file_priv)
  1902. hs = &request->file_priv->hang_stats;
  1903. if (hs) {
  1904. if (guilty) {
  1905. hs->banned = i915_context_is_banned(hs);
  1906. hs->batch_active++;
  1907. hs->guilty_ts = get_seconds();
  1908. } else {
  1909. hs->batch_pending++;
  1910. }
  1911. }
  1912. }
  1913. static void i915_gem_free_request(struct drm_i915_gem_request *request)
  1914. {
  1915. list_del(&request->list);
  1916. i915_gem_request_remove_from_client(request);
  1917. if (request->ctx)
  1918. i915_gem_context_unreference(request->ctx);
  1919. kfree(request);
  1920. }
  1921. static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
  1922. struct intel_ring_buffer *ring)
  1923. {
  1924. u32 completed_seqno;
  1925. u32 acthd;
  1926. acthd = intel_ring_get_active_head(ring);
  1927. completed_seqno = ring->get_seqno(ring, false);
  1928. while (!list_empty(&ring->request_list)) {
  1929. struct drm_i915_gem_request *request;
  1930. request = list_first_entry(&ring->request_list,
  1931. struct drm_i915_gem_request,
  1932. list);
  1933. if (request->seqno > completed_seqno)
  1934. i915_set_reset_status(ring, request, acthd);
  1935. i915_gem_free_request(request);
  1936. }
  1937. while (!list_empty(&ring->active_list)) {
  1938. struct drm_i915_gem_object *obj;
  1939. obj = list_first_entry(&ring->active_list,
  1940. struct drm_i915_gem_object,
  1941. ring_list);
  1942. i915_gem_object_move_to_inactive(obj);
  1943. }
  1944. }
  1945. void i915_gem_restore_fences(struct drm_device *dev)
  1946. {
  1947. struct drm_i915_private *dev_priv = dev->dev_private;
  1948. int i;
  1949. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  1950. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  1951. /*
  1952. * Commit delayed tiling changes if we have an object still
  1953. * attached to the fence, otherwise just clear the fence.
  1954. */
  1955. if (reg->obj) {
  1956. i915_gem_object_update_fence(reg->obj, reg,
  1957. reg->obj->tiling_mode);
  1958. } else {
  1959. i915_gem_write_fence(dev, i, NULL);
  1960. }
  1961. }
  1962. }
  1963. void i915_gem_reset(struct drm_device *dev)
  1964. {
  1965. struct drm_i915_private *dev_priv = dev->dev_private;
  1966. struct intel_ring_buffer *ring;
  1967. int i;
  1968. for_each_ring(ring, dev_priv, i)
  1969. i915_gem_reset_ring_lists(dev_priv, ring);
  1970. i915_gem_restore_fences(dev);
  1971. }
  1972. /**
  1973. * This function clears the request list as sequence numbers are passed.
  1974. */
  1975. void
  1976. i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
  1977. {
  1978. uint32_t seqno;
  1979. if (list_empty(&ring->request_list))
  1980. return;
  1981. WARN_ON(i915_verify_lists(ring->dev));
  1982. seqno = ring->get_seqno(ring, true);
  1983. while (!list_empty(&ring->request_list)) {
  1984. struct drm_i915_gem_request *request;
  1985. request = list_first_entry(&ring->request_list,
  1986. struct drm_i915_gem_request,
  1987. list);
  1988. if (!i915_seqno_passed(seqno, request->seqno))
  1989. break;
  1990. trace_i915_gem_request_retire(ring, request->seqno);
  1991. /* We know the GPU must have read the request to have
  1992. * sent us the seqno + interrupt, so use the position
  1993. * of tail of the request to update the last known position
  1994. * of the GPU head.
  1995. */
  1996. ring->last_retired_head = request->tail;
  1997. i915_gem_free_request(request);
  1998. }
  1999. /* Move any buffers on the active list that are no longer referenced
  2000. * by the ringbuffer to the flushing/inactive lists as appropriate.
  2001. */
  2002. while (!list_empty(&ring->active_list)) {
  2003. struct drm_i915_gem_object *obj;
  2004. obj = list_first_entry(&ring->active_list,
  2005. struct drm_i915_gem_object,
  2006. ring_list);
  2007. if (!i915_seqno_passed(seqno, obj->last_read_seqno))
  2008. break;
  2009. i915_gem_object_move_to_inactive(obj);
  2010. }
  2011. if (unlikely(ring->trace_irq_seqno &&
  2012. i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  2013. ring->irq_put(ring);
  2014. ring->trace_irq_seqno = 0;
  2015. }
  2016. WARN_ON(i915_verify_lists(ring->dev));
  2017. }
  2018. void
  2019. i915_gem_retire_requests(struct drm_device *dev)
  2020. {
  2021. drm_i915_private_t *dev_priv = dev->dev_private;
  2022. struct intel_ring_buffer *ring;
  2023. int i;
  2024. for_each_ring(ring, dev_priv, i)
  2025. i915_gem_retire_requests_ring(ring);
  2026. }
  2027. static void
  2028. i915_gem_retire_work_handler(struct work_struct *work)
  2029. {
  2030. drm_i915_private_t *dev_priv;
  2031. struct drm_device *dev;
  2032. struct intel_ring_buffer *ring;
  2033. bool idle;
  2034. int i;
  2035. dev_priv = container_of(work, drm_i915_private_t,
  2036. mm.retire_work.work);
  2037. dev = dev_priv->dev;
  2038. /* Come back later if the device is busy... */
  2039. if (!mutex_trylock(&dev->struct_mutex)) {
  2040. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  2041. round_jiffies_up_relative(HZ));
  2042. return;
  2043. }
  2044. i915_gem_retire_requests(dev);
  2045. /* Send a periodic flush down the ring so we don't hold onto GEM
  2046. * objects indefinitely.
  2047. */
  2048. idle = true;
  2049. for_each_ring(ring, dev_priv, i) {
  2050. if (ring->gpu_caches_dirty)
  2051. i915_add_request(ring, NULL);
  2052. idle &= list_empty(&ring->request_list);
  2053. }
  2054. if (!dev_priv->ums.mm_suspended && !idle)
  2055. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  2056. round_jiffies_up_relative(HZ));
  2057. if (idle)
  2058. intel_mark_idle(dev);
  2059. mutex_unlock(&dev->struct_mutex);
  2060. }
  2061. /**
  2062. * Ensures that an object will eventually get non-busy by flushing any required
  2063. * write domains, emitting any outstanding lazy request and retiring and
  2064. * completed requests.
  2065. */
  2066. static int
  2067. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2068. {
  2069. int ret;
  2070. if (obj->active) {
  2071. ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
  2072. if (ret)
  2073. return ret;
  2074. i915_gem_retire_requests_ring(obj->ring);
  2075. }
  2076. return 0;
  2077. }
  2078. /**
  2079. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2080. * @DRM_IOCTL_ARGS: standard ioctl arguments
  2081. *
  2082. * Returns 0 if successful, else an error is returned with the remaining time in
  2083. * the timeout parameter.
  2084. * -ETIME: object is still busy after timeout
  2085. * -ERESTARTSYS: signal interrupted the wait
  2086. * -ENONENT: object doesn't exist
  2087. * Also possible, but rare:
  2088. * -EAGAIN: GPU wedged
  2089. * -ENOMEM: damn
  2090. * -ENODEV: Internal IRQ fail
  2091. * -E?: The add request failed
  2092. *
  2093. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2094. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2095. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2096. * without holding struct_mutex the object may become re-busied before this
  2097. * function completes. A similar but shorter * race condition exists in the busy
  2098. * ioctl
  2099. */
  2100. int
  2101. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2102. {
  2103. drm_i915_private_t *dev_priv = dev->dev_private;
  2104. struct drm_i915_gem_wait *args = data;
  2105. struct drm_i915_gem_object *obj;
  2106. struct intel_ring_buffer *ring = NULL;
  2107. struct timespec timeout_stack, *timeout = NULL;
  2108. unsigned reset_counter;
  2109. u32 seqno = 0;
  2110. int ret = 0;
  2111. if (args->timeout_ns >= 0) {
  2112. timeout_stack = ns_to_timespec(args->timeout_ns);
  2113. timeout = &timeout_stack;
  2114. }
  2115. ret = i915_mutex_lock_interruptible(dev);
  2116. if (ret)
  2117. return ret;
  2118. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  2119. if (&obj->base == NULL) {
  2120. mutex_unlock(&dev->struct_mutex);
  2121. return -ENOENT;
  2122. }
  2123. /* Need to make sure the object gets inactive eventually. */
  2124. ret = i915_gem_object_flush_active(obj);
  2125. if (ret)
  2126. goto out;
  2127. if (obj->active) {
  2128. seqno = obj->last_read_seqno;
  2129. ring = obj->ring;
  2130. }
  2131. if (seqno == 0)
  2132. goto out;
  2133. /* Do this after OLR check to make sure we make forward progress polling
  2134. * on this IOCTL with a 0 timeout (like busy ioctl)
  2135. */
  2136. if (!args->timeout_ns) {
  2137. ret = -ETIME;
  2138. goto out;
  2139. }
  2140. drm_gem_object_unreference(&obj->base);
  2141. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  2142. mutex_unlock(&dev->struct_mutex);
  2143. ret = __wait_seqno(ring, seqno, reset_counter, true, timeout);
  2144. if (timeout)
  2145. args->timeout_ns = timespec_to_ns(timeout);
  2146. return ret;
  2147. out:
  2148. drm_gem_object_unreference(&obj->base);
  2149. mutex_unlock(&dev->struct_mutex);
  2150. return ret;
  2151. }
  2152. /**
  2153. * i915_gem_object_sync - sync an object to a ring.
  2154. *
  2155. * @obj: object which may be in use on another ring.
  2156. * @to: ring we wish to use the object on. May be NULL.
  2157. *
  2158. * This code is meant to abstract object synchronization with the GPU.
  2159. * Calling with NULL implies synchronizing the object with the CPU
  2160. * rather than a particular GPU ring.
  2161. *
  2162. * Returns 0 if successful, else propagates up the lower layer error.
  2163. */
  2164. int
  2165. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2166. struct intel_ring_buffer *to)
  2167. {
  2168. struct intel_ring_buffer *from = obj->ring;
  2169. u32 seqno;
  2170. int ret, idx;
  2171. if (from == NULL || to == from)
  2172. return 0;
  2173. if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  2174. return i915_gem_object_wait_rendering(obj, false);
  2175. idx = intel_ring_sync_index(from, to);
  2176. seqno = obj->last_read_seqno;
  2177. if (seqno <= from->sync_seqno[idx])
  2178. return 0;
  2179. ret = i915_gem_check_olr(obj->ring, seqno);
  2180. if (ret)
  2181. return ret;
  2182. ret = to->sync_to(to, from, seqno);
  2183. if (!ret)
  2184. /* We use last_read_seqno because sync_to()
  2185. * might have just caused seqno wrap under
  2186. * the radar.
  2187. */
  2188. from->sync_seqno[idx] = obj->last_read_seqno;
  2189. return ret;
  2190. }
  2191. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2192. {
  2193. u32 old_write_domain, old_read_domains;
  2194. /* Force a pagefault for domain tracking on next user access */
  2195. i915_gem_release_mmap(obj);
  2196. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2197. return;
  2198. /* Wait for any direct GTT access to complete */
  2199. mb();
  2200. old_read_domains = obj->base.read_domains;
  2201. old_write_domain = obj->base.write_domain;
  2202. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2203. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2204. trace_i915_gem_object_change_domain(obj,
  2205. old_read_domains,
  2206. old_write_domain);
  2207. }
  2208. int i915_vma_unbind(struct i915_vma *vma)
  2209. {
  2210. struct drm_i915_gem_object *obj = vma->obj;
  2211. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2212. int ret;
  2213. /* For now we only ever use 1 vma per object */
  2214. WARN_ON(!list_is_singular(&obj->vma_list));
  2215. if (list_empty(&vma->vma_link))
  2216. return 0;
  2217. if (!drm_mm_node_allocated(&vma->node)) {
  2218. i915_gem_vma_destroy(vma);
  2219. return 0;
  2220. }
  2221. if (obj->pin_count)
  2222. return -EBUSY;
  2223. BUG_ON(obj->pages == NULL);
  2224. ret = i915_gem_object_finish_gpu(obj);
  2225. if (ret)
  2226. return ret;
  2227. /* Continue on if we fail due to EIO, the GPU is hung so we
  2228. * should be safe and we need to cleanup or else we might
  2229. * cause memory corruption through use-after-free.
  2230. */
  2231. i915_gem_object_finish_gtt(obj);
  2232. /* release the fence reg _after_ flushing */
  2233. ret = i915_gem_object_put_fence(obj);
  2234. if (ret)
  2235. return ret;
  2236. trace_i915_vma_unbind(vma);
  2237. if (obj->has_global_gtt_mapping)
  2238. i915_gem_gtt_unbind_object(obj);
  2239. if (obj->has_aliasing_ppgtt_mapping) {
  2240. i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
  2241. obj->has_aliasing_ppgtt_mapping = 0;
  2242. }
  2243. i915_gem_gtt_finish_object(obj);
  2244. i915_gem_object_unpin_pages(obj);
  2245. list_del(&vma->mm_list);
  2246. /* Avoid an unnecessary call to unbind on rebind. */
  2247. if (i915_is_ggtt(vma->vm))
  2248. obj->map_and_fenceable = true;
  2249. drm_mm_remove_node(&vma->node);
  2250. i915_gem_vma_destroy(vma);
  2251. /* Since the unbound list is global, only move to that list if
  2252. * no more VMAs exist. */
  2253. if (list_empty(&obj->vma_list))
  2254. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2255. return 0;
  2256. }
  2257. /**
  2258. * Unbinds an object from the global GTT aperture.
  2259. */
  2260. int
  2261. i915_gem_object_ggtt_unbind(struct drm_i915_gem_object *obj)
  2262. {
  2263. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2264. struct i915_address_space *ggtt = &dev_priv->gtt.base;
  2265. if (!i915_gem_obj_ggtt_bound(obj))
  2266. return 0;
  2267. if (obj->pin_count)
  2268. return -EBUSY;
  2269. BUG_ON(obj->pages == NULL);
  2270. return i915_vma_unbind(i915_gem_obj_to_vma(obj, ggtt));
  2271. }
  2272. int i915_gpu_idle(struct drm_device *dev)
  2273. {
  2274. drm_i915_private_t *dev_priv = dev->dev_private;
  2275. struct intel_ring_buffer *ring;
  2276. int ret, i;
  2277. /* Flush everything onto the inactive list. */
  2278. for_each_ring(ring, dev_priv, i) {
  2279. ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
  2280. if (ret)
  2281. return ret;
  2282. ret = intel_ring_idle(ring);
  2283. if (ret)
  2284. return ret;
  2285. }
  2286. return 0;
  2287. }
  2288. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  2289. struct drm_i915_gem_object *obj)
  2290. {
  2291. drm_i915_private_t *dev_priv = dev->dev_private;
  2292. int fence_reg;
  2293. int fence_pitch_shift;
  2294. if (INTEL_INFO(dev)->gen >= 6) {
  2295. fence_reg = FENCE_REG_SANDYBRIDGE_0;
  2296. fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
  2297. } else {
  2298. fence_reg = FENCE_REG_965_0;
  2299. fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
  2300. }
  2301. fence_reg += reg * 8;
  2302. /* To w/a incoherency with non-atomic 64-bit register updates,
  2303. * we split the 64-bit update into two 32-bit writes. In order
  2304. * for a partial fence not to be evaluated between writes, we
  2305. * precede the update with write to turn off the fence register,
  2306. * and only enable the fence as the last step.
  2307. *
  2308. * For extra levels of paranoia, we make sure each step lands
  2309. * before applying the next step.
  2310. */
  2311. I915_WRITE(fence_reg, 0);
  2312. POSTING_READ(fence_reg);
  2313. if (obj) {
  2314. u32 size = i915_gem_obj_ggtt_size(obj);
  2315. uint64_t val;
  2316. val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
  2317. 0xfffff000) << 32;
  2318. val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
  2319. val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
  2320. if (obj->tiling_mode == I915_TILING_Y)
  2321. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2322. val |= I965_FENCE_REG_VALID;
  2323. I915_WRITE(fence_reg + 4, val >> 32);
  2324. POSTING_READ(fence_reg + 4);
  2325. I915_WRITE(fence_reg + 0, val);
  2326. POSTING_READ(fence_reg);
  2327. } else {
  2328. I915_WRITE(fence_reg + 4, 0);
  2329. POSTING_READ(fence_reg + 4);
  2330. }
  2331. }
  2332. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  2333. struct drm_i915_gem_object *obj)
  2334. {
  2335. drm_i915_private_t *dev_priv = dev->dev_private;
  2336. u32 val;
  2337. if (obj) {
  2338. u32 size = i915_gem_obj_ggtt_size(obj);
  2339. int pitch_val;
  2340. int tile_width;
  2341. WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
  2342. (size & -size) != size ||
  2343. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2344. "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  2345. i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
  2346. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  2347. tile_width = 128;
  2348. else
  2349. tile_width = 512;
  2350. /* Note: pitch better be a power of two tile widths */
  2351. pitch_val = obj->stride / tile_width;
  2352. pitch_val = ffs(pitch_val) - 1;
  2353. val = i915_gem_obj_ggtt_offset(obj);
  2354. if (obj->tiling_mode == I915_TILING_Y)
  2355. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2356. val |= I915_FENCE_SIZE_BITS(size);
  2357. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2358. val |= I830_FENCE_REG_VALID;
  2359. } else
  2360. val = 0;
  2361. if (reg < 8)
  2362. reg = FENCE_REG_830_0 + reg * 4;
  2363. else
  2364. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  2365. I915_WRITE(reg, val);
  2366. POSTING_READ(reg);
  2367. }
  2368. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  2369. struct drm_i915_gem_object *obj)
  2370. {
  2371. drm_i915_private_t *dev_priv = dev->dev_private;
  2372. uint32_t val;
  2373. if (obj) {
  2374. u32 size = i915_gem_obj_ggtt_size(obj);
  2375. uint32_t pitch_val;
  2376. WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
  2377. (size & -size) != size ||
  2378. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2379. "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
  2380. i915_gem_obj_ggtt_offset(obj), size);
  2381. pitch_val = obj->stride / 128;
  2382. pitch_val = ffs(pitch_val) - 1;
  2383. val = i915_gem_obj_ggtt_offset(obj);
  2384. if (obj->tiling_mode == I915_TILING_Y)
  2385. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2386. val |= I830_FENCE_SIZE_BITS(size);
  2387. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2388. val |= I830_FENCE_REG_VALID;
  2389. } else
  2390. val = 0;
  2391. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  2392. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  2393. }
  2394. inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
  2395. {
  2396. return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
  2397. }
  2398. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  2399. struct drm_i915_gem_object *obj)
  2400. {
  2401. struct drm_i915_private *dev_priv = dev->dev_private;
  2402. /* Ensure that all CPU reads are completed before installing a fence
  2403. * and all writes before removing the fence.
  2404. */
  2405. if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
  2406. mb();
  2407. WARN(obj && (!obj->stride || !obj->tiling_mode),
  2408. "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
  2409. obj->stride, obj->tiling_mode);
  2410. switch (INTEL_INFO(dev)->gen) {
  2411. case 7:
  2412. case 6:
  2413. case 5:
  2414. case 4: i965_write_fence_reg(dev, reg, obj); break;
  2415. case 3: i915_write_fence_reg(dev, reg, obj); break;
  2416. case 2: i830_write_fence_reg(dev, reg, obj); break;
  2417. default: BUG();
  2418. }
  2419. /* And similarly be paranoid that no direct access to this region
  2420. * is reordered to before the fence is installed.
  2421. */
  2422. if (i915_gem_object_needs_mb(obj))
  2423. mb();
  2424. }
  2425. static inline int fence_number(struct drm_i915_private *dev_priv,
  2426. struct drm_i915_fence_reg *fence)
  2427. {
  2428. return fence - dev_priv->fence_regs;
  2429. }
  2430. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  2431. struct drm_i915_fence_reg *fence,
  2432. bool enable)
  2433. {
  2434. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2435. int reg = fence_number(dev_priv, fence);
  2436. i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
  2437. if (enable) {
  2438. obj->fence_reg = reg;
  2439. fence->obj = obj;
  2440. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  2441. } else {
  2442. obj->fence_reg = I915_FENCE_REG_NONE;
  2443. fence->obj = NULL;
  2444. list_del_init(&fence->lru_list);
  2445. }
  2446. obj->fence_dirty = false;
  2447. }
  2448. static int
  2449. i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
  2450. {
  2451. if (obj->last_fenced_seqno) {
  2452. int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
  2453. if (ret)
  2454. return ret;
  2455. obj->last_fenced_seqno = 0;
  2456. }
  2457. obj->fenced_gpu_access = false;
  2458. return 0;
  2459. }
  2460. int
  2461. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2462. {
  2463. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2464. struct drm_i915_fence_reg *fence;
  2465. int ret;
  2466. ret = i915_gem_object_wait_fence(obj);
  2467. if (ret)
  2468. return ret;
  2469. if (obj->fence_reg == I915_FENCE_REG_NONE)
  2470. return 0;
  2471. fence = &dev_priv->fence_regs[obj->fence_reg];
  2472. i915_gem_object_fence_lost(obj);
  2473. i915_gem_object_update_fence(obj, fence, false);
  2474. return 0;
  2475. }
  2476. static struct drm_i915_fence_reg *
  2477. i915_find_fence_reg(struct drm_device *dev)
  2478. {
  2479. struct drm_i915_private *dev_priv = dev->dev_private;
  2480. struct drm_i915_fence_reg *reg, *avail;
  2481. int i;
  2482. /* First try to find a free reg */
  2483. avail = NULL;
  2484. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2485. reg = &dev_priv->fence_regs[i];
  2486. if (!reg->obj)
  2487. return reg;
  2488. if (!reg->pin_count)
  2489. avail = reg;
  2490. }
  2491. if (avail == NULL)
  2492. return NULL;
  2493. /* None available, try to steal one or wait for a user to finish */
  2494. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2495. if (reg->pin_count)
  2496. continue;
  2497. return reg;
  2498. }
  2499. return NULL;
  2500. }
  2501. /**
  2502. * i915_gem_object_get_fence - set up fencing for an object
  2503. * @obj: object to map through a fence reg
  2504. *
  2505. * When mapping objects through the GTT, userspace wants to be able to write
  2506. * to them without having to worry about swizzling if the object is tiled.
  2507. * This function walks the fence regs looking for a free one for @obj,
  2508. * stealing one if it can't find any.
  2509. *
  2510. * It then sets up the reg based on the object's properties: address, pitch
  2511. * and tiling format.
  2512. *
  2513. * For an untiled surface, this removes any existing fence.
  2514. */
  2515. int
  2516. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2517. {
  2518. struct drm_device *dev = obj->base.dev;
  2519. struct drm_i915_private *dev_priv = dev->dev_private;
  2520. bool enable = obj->tiling_mode != I915_TILING_NONE;
  2521. struct drm_i915_fence_reg *reg;
  2522. int ret;
  2523. /* Have we updated the tiling parameters upon the object and so
  2524. * will need to serialise the write to the associated fence register?
  2525. */
  2526. if (obj->fence_dirty) {
  2527. ret = i915_gem_object_wait_fence(obj);
  2528. if (ret)
  2529. return ret;
  2530. }
  2531. /* Just update our place in the LRU if our fence is getting reused. */
  2532. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2533. reg = &dev_priv->fence_regs[obj->fence_reg];
  2534. if (!obj->fence_dirty) {
  2535. list_move_tail(&reg->lru_list,
  2536. &dev_priv->mm.fence_list);
  2537. return 0;
  2538. }
  2539. } else if (enable) {
  2540. reg = i915_find_fence_reg(dev);
  2541. if (reg == NULL)
  2542. return -EDEADLK;
  2543. if (reg->obj) {
  2544. struct drm_i915_gem_object *old = reg->obj;
  2545. ret = i915_gem_object_wait_fence(old);
  2546. if (ret)
  2547. return ret;
  2548. i915_gem_object_fence_lost(old);
  2549. }
  2550. } else
  2551. return 0;
  2552. i915_gem_object_update_fence(obj, reg, enable);
  2553. return 0;
  2554. }
  2555. static bool i915_gem_valid_gtt_space(struct drm_device *dev,
  2556. struct drm_mm_node *gtt_space,
  2557. unsigned long cache_level)
  2558. {
  2559. struct drm_mm_node *other;
  2560. /* On non-LLC machines we have to be careful when putting differing
  2561. * types of snoopable memory together to avoid the prefetcher
  2562. * crossing memory domains and dying.
  2563. */
  2564. if (HAS_LLC(dev))
  2565. return true;
  2566. if (!drm_mm_node_allocated(gtt_space))
  2567. return true;
  2568. if (list_empty(&gtt_space->node_list))
  2569. return true;
  2570. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2571. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2572. return false;
  2573. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2574. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2575. return false;
  2576. return true;
  2577. }
  2578. static void i915_gem_verify_gtt(struct drm_device *dev)
  2579. {
  2580. #if WATCH_GTT
  2581. struct drm_i915_private *dev_priv = dev->dev_private;
  2582. struct drm_i915_gem_object *obj;
  2583. int err = 0;
  2584. list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) {
  2585. if (obj->gtt_space == NULL) {
  2586. printk(KERN_ERR "object found on GTT list with no space reserved\n");
  2587. err++;
  2588. continue;
  2589. }
  2590. if (obj->cache_level != obj->gtt_space->color) {
  2591. printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
  2592. i915_gem_obj_ggtt_offset(obj),
  2593. i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
  2594. obj->cache_level,
  2595. obj->gtt_space->color);
  2596. err++;
  2597. continue;
  2598. }
  2599. if (!i915_gem_valid_gtt_space(dev,
  2600. obj->gtt_space,
  2601. obj->cache_level)) {
  2602. printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
  2603. i915_gem_obj_ggtt_offset(obj),
  2604. i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
  2605. obj->cache_level);
  2606. err++;
  2607. continue;
  2608. }
  2609. }
  2610. WARN_ON(err);
  2611. #endif
  2612. }
  2613. /**
  2614. * Finds free space in the GTT aperture and binds the object there.
  2615. */
  2616. static int
  2617. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  2618. struct i915_address_space *vm,
  2619. unsigned alignment,
  2620. bool map_and_fenceable,
  2621. bool nonblocking)
  2622. {
  2623. struct drm_device *dev = obj->base.dev;
  2624. drm_i915_private_t *dev_priv = dev->dev_private;
  2625. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2626. size_t gtt_max =
  2627. map_and_fenceable ? dev_priv->gtt.mappable_end : vm->total;
  2628. struct i915_vma *vma;
  2629. int ret;
  2630. fence_size = i915_gem_get_gtt_size(dev,
  2631. obj->base.size,
  2632. obj->tiling_mode);
  2633. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2634. obj->base.size,
  2635. obj->tiling_mode, true);
  2636. unfenced_alignment =
  2637. i915_gem_get_gtt_alignment(dev,
  2638. obj->base.size,
  2639. obj->tiling_mode, false);
  2640. if (alignment == 0)
  2641. alignment = map_and_fenceable ? fence_alignment :
  2642. unfenced_alignment;
  2643. if (map_and_fenceable && alignment & (fence_alignment - 1)) {
  2644. DRM_ERROR("Invalid object alignment requested %u\n", alignment);
  2645. return -EINVAL;
  2646. }
  2647. size = map_and_fenceable ? fence_size : obj->base.size;
  2648. /* If the object is bigger than the entire aperture, reject it early
  2649. * before evicting everything in a vain attempt to find space.
  2650. */
  2651. if (obj->base.size > gtt_max) {
  2652. DRM_ERROR("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%zu\n",
  2653. obj->base.size,
  2654. map_and_fenceable ? "mappable" : "total",
  2655. gtt_max);
  2656. return -E2BIG;
  2657. }
  2658. ret = i915_gem_object_get_pages(obj);
  2659. if (ret)
  2660. return ret;
  2661. i915_gem_object_pin_pages(obj);
  2662. BUG_ON(!i915_is_ggtt(vm));
  2663. vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
  2664. if (IS_ERR(vma)) {
  2665. ret = PTR_ERR(vma);
  2666. goto err_unpin;
  2667. }
  2668. /* For now we only ever use 1 vma per object */
  2669. WARN_ON(!list_is_singular(&obj->vma_list));
  2670. search_free:
  2671. ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
  2672. size, alignment,
  2673. obj->cache_level, 0, gtt_max,
  2674. DRM_MM_SEARCH_DEFAULT);
  2675. if (ret) {
  2676. ret = i915_gem_evict_something(dev, vm, size, alignment,
  2677. obj->cache_level,
  2678. map_and_fenceable,
  2679. nonblocking);
  2680. if (ret == 0)
  2681. goto search_free;
  2682. goto err_free_vma;
  2683. }
  2684. if (WARN_ON(!i915_gem_valid_gtt_space(dev, &vma->node,
  2685. obj->cache_level))) {
  2686. ret = -EINVAL;
  2687. goto err_remove_node;
  2688. }
  2689. ret = i915_gem_gtt_prepare_object(obj);
  2690. if (ret)
  2691. goto err_remove_node;
  2692. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  2693. list_add_tail(&vma->mm_list, &vm->inactive_list);
  2694. if (i915_is_ggtt(vm)) {
  2695. bool mappable, fenceable;
  2696. fenceable = (vma->node.size == fence_size &&
  2697. (vma->node.start & (fence_alignment - 1)) == 0);
  2698. mappable = (vma->node.start + obj->base.size <=
  2699. dev_priv->gtt.mappable_end);
  2700. obj->map_and_fenceable = mappable && fenceable;
  2701. }
  2702. WARN_ON(map_and_fenceable && !obj->map_and_fenceable);
  2703. trace_i915_vma_bind(vma, map_and_fenceable);
  2704. i915_gem_verify_gtt(dev);
  2705. return 0;
  2706. err_remove_node:
  2707. drm_mm_remove_node(&vma->node);
  2708. err_free_vma:
  2709. i915_gem_vma_destroy(vma);
  2710. err_unpin:
  2711. i915_gem_object_unpin_pages(obj);
  2712. return ret;
  2713. }
  2714. bool
  2715. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  2716. bool force)
  2717. {
  2718. /* If we don't have a page list set up, then we're not pinned
  2719. * to GPU, and we can ignore the cache flush because it'll happen
  2720. * again at bind time.
  2721. */
  2722. if (obj->pages == NULL)
  2723. return false;
  2724. /*
  2725. * Stolen memory is always coherent with the GPU as it is explicitly
  2726. * marked as wc by the system, or the system is cache-coherent.
  2727. */
  2728. if (obj->stolen)
  2729. return false;
  2730. /* If the GPU is snooping the contents of the CPU cache,
  2731. * we do not need to manually clear the CPU cache lines. However,
  2732. * the caches are only snooped when the render cache is
  2733. * flushed/invalidated. As we always have to emit invalidations
  2734. * and flushes when moving into and out of the RENDER domain, correct
  2735. * snooping behaviour occurs naturally as the result of our domain
  2736. * tracking.
  2737. */
  2738. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  2739. return false;
  2740. trace_i915_gem_object_clflush(obj);
  2741. drm_clflush_sg(obj->pages);
  2742. return true;
  2743. }
  2744. /** Flushes the GTT write domain for the object if it's dirty. */
  2745. static void
  2746. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2747. {
  2748. uint32_t old_write_domain;
  2749. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2750. return;
  2751. /* No actual flushing is required for the GTT write domain. Writes
  2752. * to it immediately go to main memory as far as we know, so there's
  2753. * no chipset flush. It also doesn't land in render cache.
  2754. *
  2755. * However, we do have to enforce the order so that all writes through
  2756. * the GTT land before any writes to the device, such as updates to
  2757. * the GATT itself.
  2758. */
  2759. wmb();
  2760. old_write_domain = obj->base.write_domain;
  2761. obj->base.write_domain = 0;
  2762. trace_i915_gem_object_change_domain(obj,
  2763. obj->base.read_domains,
  2764. old_write_domain);
  2765. }
  2766. /** Flushes the CPU write domain for the object if it's dirty. */
  2767. static void
  2768. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj,
  2769. bool force)
  2770. {
  2771. uint32_t old_write_domain;
  2772. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2773. return;
  2774. if (i915_gem_clflush_object(obj, force))
  2775. i915_gem_chipset_flush(obj->base.dev);
  2776. old_write_domain = obj->base.write_domain;
  2777. obj->base.write_domain = 0;
  2778. trace_i915_gem_object_change_domain(obj,
  2779. obj->base.read_domains,
  2780. old_write_domain);
  2781. }
  2782. /**
  2783. * Moves a single object to the GTT read, and possibly write domain.
  2784. *
  2785. * This function returns when the move is complete, including waiting on
  2786. * flushes to occur.
  2787. */
  2788. int
  2789. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2790. {
  2791. drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
  2792. uint32_t old_write_domain, old_read_domains;
  2793. int ret;
  2794. /* Not valid to be called on unbound objects. */
  2795. if (!i915_gem_obj_bound_any(obj))
  2796. return -EINVAL;
  2797. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2798. return 0;
  2799. ret = i915_gem_object_wait_rendering(obj, !write);
  2800. if (ret)
  2801. return ret;
  2802. i915_gem_object_flush_cpu_write_domain(obj, false);
  2803. /* Serialise direct access to this object with the barriers for
  2804. * coherent writes from the GPU, by effectively invalidating the
  2805. * GTT domain upon first access.
  2806. */
  2807. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2808. mb();
  2809. old_write_domain = obj->base.write_domain;
  2810. old_read_domains = obj->base.read_domains;
  2811. /* It should now be out of any other write domains, and we can update
  2812. * the domain values for our changes.
  2813. */
  2814. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2815. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2816. if (write) {
  2817. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2818. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2819. obj->dirty = 1;
  2820. }
  2821. trace_i915_gem_object_change_domain(obj,
  2822. old_read_domains,
  2823. old_write_domain);
  2824. /* And bump the LRU for this access */
  2825. if (i915_gem_object_is_inactive(obj)) {
  2826. struct i915_vma *vma = i915_gem_obj_to_ggtt(obj);
  2827. if (vma)
  2828. list_move_tail(&vma->mm_list,
  2829. &dev_priv->gtt.base.inactive_list);
  2830. }
  2831. return 0;
  2832. }
  2833. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  2834. enum i915_cache_level cache_level)
  2835. {
  2836. struct drm_device *dev = obj->base.dev;
  2837. drm_i915_private_t *dev_priv = dev->dev_private;
  2838. struct i915_vma *vma;
  2839. int ret;
  2840. if (obj->cache_level == cache_level)
  2841. return 0;
  2842. if (obj->pin_count) {
  2843. DRM_DEBUG("can not change the cache level of pinned objects\n");
  2844. return -EBUSY;
  2845. }
  2846. list_for_each_entry(vma, &obj->vma_list, vma_link) {
  2847. if (!i915_gem_valid_gtt_space(dev, &vma->node, cache_level)) {
  2848. ret = i915_vma_unbind(vma);
  2849. if (ret)
  2850. return ret;
  2851. break;
  2852. }
  2853. }
  2854. if (i915_gem_obj_bound_any(obj)) {
  2855. ret = i915_gem_object_finish_gpu(obj);
  2856. if (ret)
  2857. return ret;
  2858. i915_gem_object_finish_gtt(obj);
  2859. /* Before SandyBridge, you could not use tiling or fence
  2860. * registers with snooped memory, so relinquish any fences
  2861. * currently pointing to our region in the aperture.
  2862. */
  2863. if (INTEL_INFO(dev)->gen < 6) {
  2864. ret = i915_gem_object_put_fence(obj);
  2865. if (ret)
  2866. return ret;
  2867. }
  2868. if (obj->has_global_gtt_mapping)
  2869. i915_gem_gtt_bind_object(obj, cache_level);
  2870. if (obj->has_aliasing_ppgtt_mapping)
  2871. i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  2872. obj, cache_level);
  2873. }
  2874. list_for_each_entry(vma, &obj->vma_list, vma_link)
  2875. vma->node.color = cache_level;
  2876. obj->cache_level = cache_level;
  2877. if (cpu_write_needs_clflush(obj)) {
  2878. u32 old_read_domains, old_write_domain;
  2879. /* If we're coming from LLC cached, then we haven't
  2880. * actually been tracking whether the data is in the
  2881. * CPU cache or not, since we only allow one bit set
  2882. * in obj->write_domain and have been skipping the clflushes.
  2883. * Just set it to the CPU cache for now.
  2884. */
  2885. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  2886. old_read_domains = obj->base.read_domains;
  2887. old_write_domain = obj->base.write_domain;
  2888. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  2889. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  2890. trace_i915_gem_object_change_domain(obj,
  2891. old_read_domains,
  2892. old_write_domain);
  2893. }
  2894. i915_gem_verify_gtt(dev);
  2895. return 0;
  2896. }
  2897. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  2898. struct drm_file *file)
  2899. {
  2900. struct drm_i915_gem_caching *args = data;
  2901. struct drm_i915_gem_object *obj;
  2902. int ret;
  2903. ret = i915_mutex_lock_interruptible(dev);
  2904. if (ret)
  2905. return ret;
  2906. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2907. if (&obj->base == NULL) {
  2908. ret = -ENOENT;
  2909. goto unlock;
  2910. }
  2911. switch (obj->cache_level) {
  2912. case I915_CACHE_LLC:
  2913. case I915_CACHE_L3_LLC:
  2914. args->caching = I915_CACHING_CACHED;
  2915. break;
  2916. case I915_CACHE_WT:
  2917. args->caching = I915_CACHING_DISPLAY;
  2918. break;
  2919. default:
  2920. args->caching = I915_CACHING_NONE;
  2921. break;
  2922. }
  2923. drm_gem_object_unreference(&obj->base);
  2924. unlock:
  2925. mutex_unlock(&dev->struct_mutex);
  2926. return ret;
  2927. }
  2928. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  2929. struct drm_file *file)
  2930. {
  2931. struct drm_i915_gem_caching *args = data;
  2932. struct drm_i915_gem_object *obj;
  2933. enum i915_cache_level level;
  2934. int ret;
  2935. switch (args->caching) {
  2936. case I915_CACHING_NONE:
  2937. level = I915_CACHE_NONE;
  2938. break;
  2939. case I915_CACHING_CACHED:
  2940. level = I915_CACHE_LLC;
  2941. break;
  2942. case I915_CACHING_DISPLAY:
  2943. level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  2944. break;
  2945. default:
  2946. return -EINVAL;
  2947. }
  2948. ret = i915_mutex_lock_interruptible(dev);
  2949. if (ret)
  2950. return ret;
  2951. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  2952. if (&obj->base == NULL) {
  2953. ret = -ENOENT;
  2954. goto unlock;
  2955. }
  2956. ret = i915_gem_object_set_cache_level(obj, level);
  2957. drm_gem_object_unreference(&obj->base);
  2958. unlock:
  2959. mutex_unlock(&dev->struct_mutex);
  2960. return ret;
  2961. }
  2962. static bool is_pin_display(struct drm_i915_gem_object *obj)
  2963. {
  2964. /* There are 3 sources that pin objects:
  2965. * 1. The display engine (scanouts, sprites, cursors);
  2966. * 2. Reservations for execbuffer;
  2967. * 3. The user.
  2968. *
  2969. * We can ignore reservations as we hold the struct_mutex and
  2970. * are only called outside of the reservation path. The user
  2971. * can only increment pin_count once, and so if after
  2972. * subtracting the potential reference by the user, any pin_count
  2973. * remains, it must be due to another use by the display engine.
  2974. */
  2975. return obj->pin_count - !!obj->user_pin_count;
  2976. }
  2977. /*
  2978. * Prepare buffer for display plane (scanout, cursors, etc).
  2979. * Can be called from an uninterruptible phase (modesetting) and allows
  2980. * any flushes to be pipelined (for pageflips).
  2981. */
  2982. int
  2983. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  2984. u32 alignment,
  2985. struct intel_ring_buffer *pipelined)
  2986. {
  2987. u32 old_read_domains, old_write_domain;
  2988. int ret;
  2989. if (pipelined != obj->ring) {
  2990. ret = i915_gem_object_sync(obj, pipelined);
  2991. if (ret)
  2992. return ret;
  2993. }
  2994. /* Mark the pin_display early so that we account for the
  2995. * display coherency whilst setting up the cache domains.
  2996. */
  2997. obj->pin_display = true;
  2998. /* The display engine is not coherent with the LLC cache on gen6. As
  2999. * a result, we make sure that the pinning that is about to occur is
  3000. * done with uncached PTEs. This is lowest common denominator for all
  3001. * chipsets.
  3002. *
  3003. * However for gen6+, we could do better by using the GFDT bit instead
  3004. * of uncaching, which would allow us to flush all the LLC-cached data
  3005. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3006. */
  3007. ret = i915_gem_object_set_cache_level(obj,
  3008. HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  3009. if (ret)
  3010. goto err_unpin_display;
  3011. /* As the user may map the buffer once pinned in the display plane
  3012. * (e.g. libkms for the bootup splash), we have to ensure that we
  3013. * always use map_and_fenceable for all scanout buffers.
  3014. */
  3015. ret = i915_gem_obj_ggtt_pin(obj, alignment, true, false);
  3016. if (ret)
  3017. goto err_unpin_display;
  3018. i915_gem_object_flush_cpu_write_domain(obj, true);
  3019. old_write_domain = obj->base.write_domain;
  3020. old_read_domains = obj->base.read_domains;
  3021. /* It should now be out of any other write domains, and we can update
  3022. * the domain values for our changes.
  3023. */
  3024. obj->base.write_domain = 0;
  3025. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3026. trace_i915_gem_object_change_domain(obj,
  3027. old_read_domains,
  3028. old_write_domain);
  3029. return 0;
  3030. err_unpin_display:
  3031. obj->pin_display = is_pin_display(obj);
  3032. return ret;
  3033. }
  3034. void
  3035. i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj)
  3036. {
  3037. i915_gem_object_unpin(obj);
  3038. obj->pin_display = is_pin_display(obj);
  3039. }
  3040. int
  3041. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  3042. {
  3043. int ret;
  3044. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  3045. return 0;
  3046. ret = i915_gem_object_wait_rendering(obj, false);
  3047. if (ret)
  3048. return ret;
  3049. /* Ensure that we invalidate the GPU's caches and TLBs. */
  3050. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  3051. return 0;
  3052. }
  3053. /**
  3054. * Moves a single object to the CPU read, and possibly write domain.
  3055. *
  3056. * This function returns when the move is complete, including waiting on
  3057. * flushes to occur.
  3058. */
  3059. int
  3060. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3061. {
  3062. uint32_t old_write_domain, old_read_domains;
  3063. int ret;
  3064. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3065. return 0;
  3066. ret = i915_gem_object_wait_rendering(obj, !write);
  3067. if (ret)
  3068. return ret;
  3069. i915_gem_object_flush_gtt_write_domain(obj);
  3070. old_write_domain = obj->base.write_domain;
  3071. old_read_domains = obj->base.read_domains;
  3072. /* Flush the CPU cache if it's still invalid. */
  3073. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3074. i915_gem_clflush_object(obj, false);
  3075. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3076. }
  3077. /* It should now be out of any other write domains, and we can update
  3078. * the domain values for our changes.
  3079. */
  3080. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3081. /* If we're writing through the CPU, then the GPU read domains will
  3082. * need to be invalidated at next use.
  3083. */
  3084. if (write) {
  3085. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3086. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3087. }
  3088. trace_i915_gem_object_change_domain(obj,
  3089. old_read_domains,
  3090. old_write_domain);
  3091. return 0;
  3092. }
  3093. /* Throttle our rendering by waiting until the ring has completed our requests
  3094. * emitted over 20 msec ago.
  3095. *
  3096. * Note that if we were to use the current jiffies each time around the loop,
  3097. * we wouldn't escape the function with any frames outstanding if the time to
  3098. * render a frame was over 20ms.
  3099. *
  3100. * This should get us reasonable parallelism between CPU and GPU but also
  3101. * relatively low latency when blocking on a particular request to finish.
  3102. */
  3103. static int
  3104. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3105. {
  3106. struct drm_i915_private *dev_priv = dev->dev_private;
  3107. struct drm_i915_file_private *file_priv = file->driver_priv;
  3108. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  3109. struct drm_i915_gem_request *request;
  3110. struct intel_ring_buffer *ring = NULL;
  3111. unsigned reset_counter;
  3112. u32 seqno = 0;
  3113. int ret;
  3114. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3115. if (ret)
  3116. return ret;
  3117. ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
  3118. if (ret)
  3119. return ret;
  3120. spin_lock(&file_priv->mm.lock);
  3121. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3122. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3123. break;
  3124. ring = request->ring;
  3125. seqno = request->seqno;
  3126. }
  3127. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  3128. spin_unlock(&file_priv->mm.lock);
  3129. if (seqno == 0)
  3130. return 0;
  3131. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
  3132. if (ret == 0)
  3133. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3134. return ret;
  3135. }
  3136. int
  3137. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3138. struct i915_address_space *vm,
  3139. uint32_t alignment,
  3140. bool map_and_fenceable,
  3141. bool nonblocking)
  3142. {
  3143. struct i915_vma *vma;
  3144. int ret;
  3145. if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3146. return -EBUSY;
  3147. WARN_ON(map_and_fenceable && !i915_is_ggtt(vm));
  3148. vma = i915_gem_obj_to_vma(obj, vm);
  3149. if (vma) {
  3150. if ((alignment &&
  3151. vma->node.start & (alignment - 1)) ||
  3152. (map_and_fenceable && !obj->map_and_fenceable)) {
  3153. WARN(obj->pin_count,
  3154. "bo is already pinned with incorrect alignment:"
  3155. " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
  3156. " obj->map_and_fenceable=%d\n",
  3157. i915_gem_obj_offset(obj, vm), alignment,
  3158. map_and_fenceable,
  3159. obj->map_and_fenceable);
  3160. ret = i915_vma_unbind(vma);
  3161. if (ret)
  3162. return ret;
  3163. }
  3164. }
  3165. if (!i915_gem_obj_bound(obj, vm)) {
  3166. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3167. ret = i915_gem_object_bind_to_vm(obj, vm, alignment,
  3168. map_and_fenceable,
  3169. nonblocking);
  3170. if (ret)
  3171. return ret;
  3172. if (!dev_priv->mm.aliasing_ppgtt)
  3173. i915_gem_gtt_bind_object(obj, obj->cache_level);
  3174. }
  3175. if (!obj->has_global_gtt_mapping && map_and_fenceable)
  3176. i915_gem_gtt_bind_object(obj, obj->cache_level);
  3177. obj->pin_count++;
  3178. obj->pin_mappable |= map_and_fenceable;
  3179. return 0;
  3180. }
  3181. void
  3182. i915_gem_object_unpin(struct drm_i915_gem_object *obj)
  3183. {
  3184. BUG_ON(obj->pin_count == 0);
  3185. BUG_ON(!i915_gem_obj_bound_any(obj));
  3186. if (--obj->pin_count == 0)
  3187. obj->pin_mappable = false;
  3188. }
  3189. int
  3190. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3191. struct drm_file *file)
  3192. {
  3193. struct drm_i915_gem_pin *args = data;
  3194. struct drm_i915_gem_object *obj;
  3195. int ret;
  3196. ret = i915_mutex_lock_interruptible(dev);
  3197. if (ret)
  3198. return ret;
  3199. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3200. if (&obj->base == NULL) {
  3201. ret = -ENOENT;
  3202. goto unlock;
  3203. }
  3204. if (obj->madv != I915_MADV_WILLNEED) {
  3205. DRM_ERROR("Attempting to pin a purgeable buffer\n");
  3206. ret = -EINVAL;
  3207. goto out;
  3208. }
  3209. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  3210. DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3211. args->handle);
  3212. ret = -EINVAL;
  3213. goto out;
  3214. }
  3215. if (obj->user_pin_count == 0) {
  3216. ret = i915_gem_obj_ggtt_pin(obj, args->alignment, true, false);
  3217. if (ret)
  3218. goto out;
  3219. }
  3220. obj->user_pin_count++;
  3221. obj->pin_filp = file;
  3222. args->offset = i915_gem_obj_ggtt_offset(obj);
  3223. out:
  3224. drm_gem_object_unreference(&obj->base);
  3225. unlock:
  3226. mutex_unlock(&dev->struct_mutex);
  3227. return ret;
  3228. }
  3229. int
  3230. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  3231. struct drm_file *file)
  3232. {
  3233. struct drm_i915_gem_pin *args = data;
  3234. struct drm_i915_gem_object *obj;
  3235. int ret;
  3236. ret = i915_mutex_lock_interruptible(dev);
  3237. if (ret)
  3238. return ret;
  3239. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3240. if (&obj->base == NULL) {
  3241. ret = -ENOENT;
  3242. goto unlock;
  3243. }
  3244. if (obj->pin_filp != file) {
  3245. DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3246. args->handle);
  3247. ret = -EINVAL;
  3248. goto out;
  3249. }
  3250. obj->user_pin_count--;
  3251. if (obj->user_pin_count == 0) {
  3252. obj->pin_filp = NULL;
  3253. i915_gem_object_unpin(obj);
  3254. }
  3255. out:
  3256. drm_gem_object_unreference(&obj->base);
  3257. unlock:
  3258. mutex_unlock(&dev->struct_mutex);
  3259. return ret;
  3260. }
  3261. int
  3262. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3263. struct drm_file *file)
  3264. {
  3265. struct drm_i915_gem_busy *args = data;
  3266. struct drm_i915_gem_object *obj;
  3267. int ret;
  3268. ret = i915_mutex_lock_interruptible(dev);
  3269. if (ret)
  3270. return ret;
  3271. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3272. if (&obj->base == NULL) {
  3273. ret = -ENOENT;
  3274. goto unlock;
  3275. }
  3276. /* Count all active objects as busy, even if they are currently not used
  3277. * by the gpu. Users of this interface expect objects to eventually
  3278. * become non-busy without any further actions, therefore emit any
  3279. * necessary flushes here.
  3280. */
  3281. ret = i915_gem_object_flush_active(obj);
  3282. args->busy = obj->active;
  3283. if (obj->ring) {
  3284. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  3285. args->busy |= intel_ring_flag(obj->ring) << 16;
  3286. }
  3287. drm_gem_object_unreference(&obj->base);
  3288. unlock:
  3289. mutex_unlock(&dev->struct_mutex);
  3290. return ret;
  3291. }
  3292. int
  3293. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3294. struct drm_file *file_priv)
  3295. {
  3296. return i915_gem_ring_throttle(dev, file_priv);
  3297. }
  3298. int
  3299. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3300. struct drm_file *file_priv)
  3301. {
  3302. struct drm_i915_gem_madvise *args = data;
  3303. struct drm_i915_gem_object *obj;
  3304. int ret;
  3305. switch (args->madv) {
  3306. case I915_MADV_DONTNEED:
  3307. case I915_MADV_WILLNEED:
  3308. break;
  3309. default:
  3310. return -EINVAL;
  3311. }
  3312. ret = i915_mutex_lock_interruptible(dev);
  3313. if (ret)
  3314. return ret;
  3315. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3316. if (&obj->base == NULL) {
  3317. ret = -ENOENT;
  3318. goto unlock;
  3319. }
  3320. if (obj->pin_count) {
  3321. ret = -EINVAL;
  3322. goto out;
  3323. }
  3324. if (obj->madv != __I915_MADV_PURGED)
  3325. obj->madv = args->madv;
  3326. /* if the object is no longer attached, discard its backing storage */
  3327. if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
  3328. i915_gem_object_truncate(obj);
  3329. args->retained = obj->madv != __I915_MADV_PURGED;
  3330. out:
  3331. drm_gem_object_unreference(&obj->base);
  3332. unlock:
  3333. mutex_unlock(&dev->struct_mutex);
  3334. return ret;
  3335. }
  3336. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3337. const struct drm_i915_gem_object_ops *ops)
  3338. {
  3339. INIT_LIST_HEAD(&obj->global_list);
  3340. INIT_LIST_HEAD(&obj->ring_list);
  3341. INIT_LIST_HEAD(&obj->obj_exec_link);
  3342. INIT_LIST_HEAD(&obj->vma_list);
  3343. obj->ops = ops;
  3344. obj->fence_reg = I915_FENCE_REG_NONE;
  3345. obj->madv = I915_MADV_WILLNEED;
  3346. /* Avoid an unnecessary call to unbind on the first bind. */
  3347. obj->map_and_fenceable = true;
  3348. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3349. }
  3350. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3351. .get_pages = i915_gem_object_get_pages_gtt,
  3352. .put_pages = i915_gem_object_put_pages_gtt,
  3353. };
  3354. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3355. size_t size)
  3356. {
  3357. struct drm_i915_gem_object *obj;
  3358. struct address_space *mapping;
  3359. gfp_t mask;
  3360. obj = i915_gem_object_alloc(dev);
  3361. if (obj == NULL)
  3362. return NULL;
  3363. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3364. i915_gem_object_free(obj);
  3365. return NULL;
  3366. }
  3367. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3368. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3369. /* 965gm cannot relocate objects above 4GiB. */
  3370. mask &= ~__GFP_HIGHMEM;
  3371. mask |= __GFP_DMA32;
  3372. }
  3373. mapping = file_inode(obj->base.filp)->i_mapping;
  3374. mapping_set_gfp_mask(mapping, mask);
  3375. i915_gem_object_init(obj, &i915_gem_object_ops);
  3376. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3377. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3378. if (HAS_LLC(dev)) {
  3379. /* On some devices, we can have the GPU use the LLC (the CPU
  3380. * cache) for about a 10% performance improvement
  3381. * compared to uncached. Graphics requests other than
  3382. * display scanout are coherent with the CPU in
  3383. * accessing this cache. This means in this mode we
  3384. * don't need to clflush on the CPU side, and on the
  3385. * GPU side we only need to flush internal caches to
  3386. * get data visible to the CPU.
  3387. *
  3388. * However, we maintain the display planes as UC, and so
  3389. * need to rebind when first used as such.
  3390. */
  3391. obj->cache_level = I915_CACHE_LLC;
  3392. } else
  3393. obj->cache_level = I915_CACHE_NONE;
  3394. trace_i915_gem_object_create(obj);
  3395. return obj;
  3396. }
  3397. int i915_gem_init_object(struct drm_gem_object *obj)
  3398. {
  3399. BUG();
  3400. return 0;
  3401. }
  3402. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3403. {
  3404. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3405. struct drm_device *dev = obj->base.dev;
  3406. drm_i915_private_t *dev_priv = dev->dev_private;
  3407. struct i915_vma *vma, *next;
  3408. trace_i915_gem_object_destroy(obj);
  3409. if (obj->phys_obj)
  3410. i915_gem_detach_phys_object(dev, obj);
  3411. obj->pin_count = 0;
  3412. /* NB: 0 or 1 elements */
  3413. WARN_ON(!list_empty(&obj->vma_list) &&
  3414. !list_is_singular(&obj->vma_list));
  3415. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3416. int ret = i915_vma_unbind(vma);
  3417. if (WARN_ON(ret == -ERESTARTSYS)) {
  3418. bool was_interruptible;
  3419. was_interruptible = dev_priv->mm.interruptible;
  3420. dev_priv->mm.interruptible = false;
  3421. WARN_ON(i915_vma_unbind(vma));
  3422. dev_priv->mm.interruptible = was_interruptible;
  3423. }
  3424. }
  3425. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3426. * before progressing. */
  3427. if (obj->stolen)
  3428. i915_gem_object_unpin_pages(obj);
  3429. if (WARN_ON(obj->pages_pin_count))
  3430. obj->pages_pin_count = 0;
  3431. i915_gem_object_put_pages(obj);
  3432. i915_gem_object_free_mmap_offset(obj);
  3433. i915_gem_object_release_stolen(obj);
  3434. BUG_ON(obj->pages);
  3435. if (obj->base.import_attach)
  3436. drm_prime_gem_destroy(&obj->base, NULL);
  3437. drm_gem_object_release(&obj->base);
  3438. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3439. kfree(obj->bit_17);
  3440. i915_gem_object_free(obj);
  3441. }
  3442. struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
  3443. struct i915_address_space *vm)
  3444. {
  3445. struct i915_vma *vma;
  3446. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3447. if (vma->vm == vm)
  3448. return vma;
  3449. return NULL;
  3450. }
  3451. static struct i915_vma *__i915_gem_vma_create(struct drm_i915_gem_object *obj,
  3452. struct i915_address_space *vm)
  3453. {
  3454. struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
  3455. if (vma == NULL)
  3456. return ERR_PTR(-ENOMEM);
  3457. INIT_LIST_HEAD(&vma->vma_link);
  3458. INIT_LIST_HEAD(&vma->mm_list);
  3459. INIT_LIST_HEAD(&vma->exec_list);
  3460. vma->vm = vm;
  3461. vma->obj = obj;
  3462. /* Keep GGTT vmas first to make debug easier */
  3463. if (i915_is_ggtt(vm))
  3464. list_add(&vma->vma_link, &obj->vma_list);
  3465. else
  3466. list_add_tail(&vma->vma_link, &obj->vma_list);
  3467. return vma;
  3468. }
  3469. struct i915_vma *
  3470. i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
  3471. struct i915_address_space *vm)
  3472. {
  3473. struct i915_vma *vma;
  3474. vma = i915_gem_obj_to_vma(obj, vm);
  3475. if (!vma)
  3476. vma = __i915_gem_vma_create(obj, vm);
  3477. return vma;
  3478. }
  3479. void i915_gem_vma_destroy(struct i915_vma *vma)
  3480. {
  3481. WARN_ON(vma->node.allocated);
  3482. /* Keep the vma as a placeholder in the execbuffer reservation lists */
  3483. if (!list_empty(&vma->exec_list))
  3484. return;
  3485. list_del(&vma->vma_link);
  3486. kfree(vma);
  3487. }
  3488. int
  3489. i915_gem_idle(struct drm_device *dev)
  3490. {
  3491. drm_i915_private_t *dev_priv = dev->dev_private;
  3492. int ret;
  3493. if (dev_priv->ums.mm_suspended)
  3494. return 0;
  3495. ret = i915_gpu_idle(dev);
  3496. if (ret)
  3497. return ret;
  3498. i915_gem_retire_requests(dev);
  3499. /* Under UMS, be paranoid and evict. */
  3500. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3501. i915_gem_evict_everything(dev);
  3502. del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
  3503. i915_kernel_lost_context(dev);
  3504. i915_gem_cleanup_ringbuffer(dev);
  3505. /* Cancel the retire work handler, which should be idle now. */
  3506. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3507. return 0;
  3508. }
  3509. int i915_gem_l3_remap(struct intel_ring_buffer *ring, int slice)
  3510. {
  3511. struct drm_device *dev = ring->dev;
  3512. drm_i915_private_t *dev_priv = dev->dev_private;
  3513. u32 reg_base = GEN7_L3LOG_BASE + (slice * 0x200);
  3514. u32 *remap_info = dev_priv->l3_parity.remap_info[slice];
  3515. int i, ret;
  3516. if (!HAS_L3_DPF(dev) || !remap_info)
  3517. return 0;
  3518. ret = intel_ring_begin(ring, GEN7_L3LOG_SIZE / 4 * 3);
  3519. if (ret)
  3520. return ret;
  3521. /*
  3522. * Note: We do not worry about the concurrent register cacheline hang
  3523. * here because no other code should access these registers other than
  3524. * at initialization time.
  3525. */
  3526. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  3527. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  3528. intel_ring_emit(ring, reg_base + i);
  3529. intel_ring_emit(ring, remap_info[i/4]);
  3530. }
  3531. intel_ring_advance(ring);
  3532. return ret;
  3533. }
  3534. void i915_gem_init_swizzling(struct drm_device *dev)
  3535. {
  3536. drm_i915_private_t *dev_priv = dev->dev_private;
  3537. if (INTEL_INFO(dev)->gen < 5 ||
  3538. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3539. return;
  3540. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3541. DISP_TILE_SURFACE_SWIZZLING);
  3542. if (IS_GEN5(dev))
  3543. return;
  3544. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3545. if (IS_GEN6(dev))
  3546. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3547. else if (IS_GEN7(dev))
  3548. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3549. else
  3550. BUG();
  3551. }
  3552. static bool
  3553. intel_enable_blt(struct drm_device *dev)
  3554. {
  3555. if (!HAS_BLT(dev))
  3556. return false;
  3557. /* The blitter was dysfunctional on early prototypes */
  3558. if (IS_GEN6(dev) && dev->pdev->revision < 8) {
  3559. DRM_INFO("BLT not supported on this pre-production hardware;"
  3560. " graphics performance will be degraded.\n");
  3561. return false;
  3562. }
  3563. return true;
  3564. }
  3565. static int i915_gem_init_rings(struct drm_device *dev)
  3566. {
  3567. struct drm_i915_private *dev_priv = dev->dev_private;
  3568. int ret;
  3569. ret = intel_init_render_ring_buffer(dev);
  3570. if (ret)
  3571. return ret;
  3572. if (HAS_BSD(dev)) {
  3573. ret = intel_init_bsd_ring_buffer(dev);
  3574. if (ret)
  3575. goto cleanup_render_ring;
  3576. }
  3577. if (intel_enable_blt(dev)) {
  3578. ret = intel_init_blt_ring_buffer(dev);
  3579. if (ret)
  3580. goto cleanup_bsd_ring;
  3581. }
  3582. if (HAS_VEBOX(dev)) {
  3583. ret = intel_init_vebox_ring_buffer(dev);
  3584. if (ret)
  3585. goto cleanup_blt_ring;
  3586. }
  3587. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  3588. if (ret)
  3589. goto cleanup_vebox_ring;
  3590. return 0;
  3591. cleanup_vebox_ring:
  3592. intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
  3593. cleanup_blt_ring:
  3594. intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
  3595. cleanup_bsd_ring:
  3596. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3597. cleanup_render_ring:
  3598. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3599. return ret;
  3600. }
  3601. int
  3602. i915_gem_init_hw(struct drm_device *dev)
  3603. {
  3604. drm_i915_private_t *dev_priv = dev->dev_private;
  3605. int ret, i;
  3606. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  3607. return -EIO;
  3608. if (dev_priv->ellc_size)
  3609. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3610. if (IS_HSW_GT3(dev))
  3611. I915_WRITE(MI_PREDICATE_RESULT_2, LOWER_SLICE_ENABLED);
  3612. else
  3613. I915_WRITE(MI_PREDICATE_RESULT_2, LOWER_SLICE_DISABLED);
  3614. if (HAS_PCH_NOP(dev)) {
  3615. u32 temp = I915_READ(GEN7_MSG_CTL);
  3616. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3617. I915_WRITE(GEN7_MSG_CTL, temp);
  3618. }
  3619. i915_gem_init_swizzling(dev);
  3620. ret = i915_gem_init_rings(dev);
  3621. if (ret)
  3622. return ret;
  3623. for (i = 0; i < NUM_L3_SLICES(dev); i++)
  3624. i915_gem_l3_remap(&dev_priv->ring[RCS], i);
  3625. /*
  3626. * XXX: There was some w/a described somewhere suggesting loading
  3627. * contexts before PPGTT.
  3628. */
  3629. i915_gem_context_init(dev);
  3630. if (dev_priv->mm.aliasing_ppgtt) {
  3631. ret = dev_priv->mm.aliasing_ppgtt->enable(dev);
  3632. if (ret) {
  3633. i915_gem_cleanup_aliasing_ppgtt(dev);
  3634. DRM_INFO("PPGTT enable failed. This is not fatal, but unexpected\n");
  3635. }
  3636. }
  3637. return 0;
  3638. }
  3639. int i915_gem_init(struct drm_device *dev)
  3640. {
  3641. struct drm_i915_private *dev_priv = dev->dev_private;
  3642. int ret;
  3643. mutex_lock(&dev->struct_mutex);
  3644. if (IS_VALLEYVIEW(dev)) {
  3645. /* VLVA0 (potential hack), BIOS isn't actually waking us */
  3646. I915_WRITE(VLV_GTLC_WAKE_CTRL, 1);
  3647. if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10))
  3648. DRM_DEBUG_DRIVER("allow wake ack timed out\n");
  3649. }
  3650. i915_gem_init_global_gtt(dev);
  3651. ret = i915_gem_init_hw(dev);
  3652. mutex_unlock(&dev->struct_mutex);
  3653. if (ret) {
  3654. i915_gem_cleanup_aliasing_ppgtt(dev);
  3655. return ret;
  3656. }
  3657. /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
  3658. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3659. dev_priv->dri1.allow_batchbuffer = 1;
  3660. return 0;
  3661. }
  3662. void
  3663. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3664. {
  3665. drm_i915_private_t *dev_priv = dev->dev_private;
  3666. struct intel_ring_buffer *ring;
  3667. int i;
  3668. for_each_ring(ring, dev_priv, i)
  3669. intel_cleanup_ring_buffer(ring);
  3670. }
  3671. int
  3672. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3673. struct drm_file *file_priv)
  3674. {
  3675. struct drm_i915_private *dev_priv = dev->dev_private;
  3676. int ret;
  3677. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3678. return 0;
  3679. if (i915_reset_in_progress(&dev_priv->gpu_error)) {
  3680. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3681. atomic_set(&dev_priv->gpu_error.reset_counter, 0);
  3682. }
  3683. mutex_lock(&dev->struct_mutex);
  3684. dev_priv->ums.mm_suspended = 0;
  3685. ret = i915_gem_init_hw(dev);
  3686. if (ret != 0) {
  3687. mutex_unlock(&dev->struct_mutex);
  3688. return ret;
  3689. }
  3690. BUG_ON(!list_empty(&dev_priv->gtt.base.active_list));
  3691. mutex_unlock(&dev->struct_mutex);
  3692. ret = drm_irq_install(dev);
  3693. if (ret)
  3694. goto cleanup_ringbuffer;
  3695. return 0;
  3696. cleanup_ringbuffer:
  3697. mutex_lock(&dev->struct_mutex);
  3698. i915_gem_cleanup_ringbuffer(dev);
  3699. dev_priv->ums.mm_suspended = 1;
  3700. mutex_unlock(&dev->struct_mutex);
  3701. return ret;
  3702. }
  3703. int
  3704. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3705. struct drm_file *file_priv)
  3706. {
  3707. struct drm_i915_private *dev_priv = dev->dev_private;
  3708. int ret;
  3709. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3710. return 0;
  3711. drm_irq_uninstall(dev);
  3712. mutex_lock(&dev->struct_mutex);
  3713. ret = i915_gem_idle(dev);
  3714. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3715. * We need to replace this with a semaphore, or something.
  3716. * And not confound ums.mm_suspended!
  3717. */
  3718. if (ret != 0)
  3719. dev_priv->ums.mm_suspended = 1;
  3720. mutex_unlock(&dev->struct_mutex);
  3721. return ret;
  3722. }
  3723. void
  3724. i915_gem_lastclose(struct drm_device *dev)
  3725. {
  3726. int ret;
  3727. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3728. return;
  3729. mutex_lock(&dev->struct_mutex);
  3730. ret = i915_gem_idle(dev);
  3731. if (ret)
  3732. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3733. mutex_unlock(&dev->struct_mutex);
  3734. }
  3735. static void
  3736. init_ring_lists(struct intel_ring_buffer *ring)
  3737. {
  3738. INIT_LIST_HEAD(&ring->active_list);
  3739. INIT_LIST_HEAD(&ring->request_list);
  3740. }
  3741. static void i915_init_vm(struct drm_i915_private *dev_priv,
  3742. struct i915_address_space *vm)
  3743. {
  3744. vm->dev = dev_priv->dev;
  3745. INIT_LIST_HEAD(&vm->active_list);
  3746. INIT_LIST_HEAD(&vm->inactive_list);
  3747. INIT_LIST_HEAD(&vm->global_link);
  3748. list_add(&vm->global_link, &dev_priv->vm_list);
  3749. }
  3750. void
  3751. i915_gem_load(struct drm_device *dev)
  3752. {
  3753. drm_i915_private_t *dev_priv = dev->dev_private;
  3754. int i;
  3755. dev_priv->slab =
  3756. kmem_cache_create("i915_gem_object",
  3757. sizeof(struct drm_i915_gem_object), 0,
  3758. SLAB_HWCACHE_ALIGN,
  3759. NULL);
  3760. INIT_LIST_HEAD(&dev_priv->vm_list);
  3761. i915_init_vm(dev_priv, &dev_priv->gtt.base);
  3762. INIT_LIST_HEAD(&dev_priv->context_list);
  3763. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  3764. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  3765. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3766. for (i = 0; i < I915_NUM_RINGS; i++)
  3767. init_ring_lists(&dev_priv->ring[i]);
  3768. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  3769. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  3770. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  3771. i915_gem_retire_work_handler);
  3772. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  3773. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  3774. if (IS_GEN3(dev)) {
  3775. I915_WRITE(MI_ARB_STATE,
  3776. _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  3777. }
  3778. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  3779. /* Old X drivers will take 0-2 for front, back, depth buffers */
  3780. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3781. dev_priv->fence_reg_start = 3;
  3782. if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
  3783. dev_priv->num_fence_regs = 32;
  3784. else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  3785. dev_priv->num_fence_regs = 16;
  3786. else
  3787. dev_priv->num_fence_regs = 8;
  3788. /* Initialize fence registers to zero */
  3789. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  3790. i915_gem_restore_fences(dev);
  3791. i915_gem_detect_bit_6_swizzle(dev);
  3792. init_waitqueue_head(&dev_priv->pending_flip_queue);
  3793. dev_priv->mm.interruptible = true;
  3794. dev_priv->mm.inactive_shrinker.scan_objects = i915_gem_inactive_scan;
  3795. dev_priv->mm.inactive_shrinker.count_objects = i915_gem_inactive_count;
  3796. dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
  3797. register_shrinker(&dev_priv->mm.inactive_shrinker);
  3798. }
  3799. /*
  3800. * Create a physically contiguous memory object for this object
  3801. * e.g. for cursor + overlay regs
  3802. */
  3803. static int i915_gem_init_phys_object(struct drm_device *dev,
  3804. int id, int size, int align)
  3805. {
  3806. drm_i915_private_t *dev_priv = dev->dev_private;
  3807. struct drm_i915_gem_phys_object *phys_obj;
  3808. int ret;
  3809. if (dev_priv->mm.phys_objs[id - 1] || !size)
  3810. return 0;
  3811. phys_obj = kzalloc(sizeof(*phys_obj), GFP_KERNEL);
  3812. if (!phys_obj)
  3813. return -ENOMEM;
  3814. phys_obj->id = id;
  3815. phys_obj->handle = drm_pci_alloc(dev, size, align);
  3816. if (!phys_obj->handle) {
  3817. ret = -ENOMEM;
  3818. goto kfree_obj;
  3819. }
  3820. #ifdef CONFIG_X86
  3821. set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3822. #endif
  3823. dev_priv->mm.phys_objs[id - 1] = phys_obj;
  3824. return 0;
  3825. kfree_obj:
  3826. kfree(phys_obj);
  3827. return ret;
  3828. }
  3829. static void i915_gem_free_phys_object(struct drm_device *dev, int id)
  3830. {
  3831. drm_i915_private_t *dev_priv = dev->dev_private;
  3832. struct drm_i915_gem_phys_object *phys_obj;
  3833. if (!dev_priv->mm.phys_objs[id - 1])
  3834. return;
  3835. phys_obj = dev_priv->mm.phys_objs[id - 1];
  3836. if (phys_obj->cur_obj) {
  3837. i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
  3838. }
  3839. #ifdef CONFIG_X86
  3840. set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
  3841. #endif
  3842. drm_pci_free(dev, phys_obj->handle);
  3843. kfree(phys_obj);
  3844. dev_priv->mm.phys_objs[id - 1] = NULL;
  3845. }
  3846. void i915_gem_free_all_phys_object(struct drm_device *dev)
  3847. {
  3848. int i;
  3849. for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
  3850. i915_gem_free_phys_object(dev, i);
  3851. }
  3852. void i915_gem_detach_phys_object(struct drm_device *dev,
  3853. struct drm_i915_gem_object *obj)
  3854. {
  3855. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  3856. char *vaddr;
  3857. int i;
  3858. int page_count;
  3859. if (!obj->phys_obj)
  3860. return;
  3861. vaddr = obj->phys_obj->handle->vaddr;
  3862. page_count = obj->base.size / PAGE_SIZE;
  3863. for (i = 0; i < page_count; i++) {
  3864. struct page *page = shmem_read_mapping_page(mapping, i);
  3865. if (!IS_ERR(page)) {
  3866. char *dst = kmap_atomic(page);
  3867. memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
  3868. kunmap_atomic(dst);
  3869. drm_clflush_pages(&page, 1);
  3870. set_page_dirty(page);
  3871. mark_page_accessed(page);
  3872. page_cache_release(page);
  3873. }
  3874. }
  3875. i915_gem_chipset_flush(dev);
  3876. obj->phys_obj->cur_obj = NULL;
  3877. obj->phys_obj = NULL;
  3878. }
  3879. int
  3880. i915_gem_attach_phys_object(struct drm_device *dev,
  3881. struct drm_i915_gem_object *obj,
  3882. int id,
  3883. int align)
  3884. {
  3885. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  3886. drm_i915_private_t *dev_priv = dev->dev_private;
  3887. int ret = 0;
  3888. int page_count;
  3889. int i;
  3890. if (id > I915_MAX_PHYS_OBJECT)
  3891. return -EINVAL;
  3892. if (obj->phys_obj) {
  3893. if (obj->phys_obj->id == id)
  3894. return 0;
  3895. i915_gem_detach_phys_object(dev, obj);
  3896. }
  3897. /* create a new object */
  3898. if (!dev_priv->mm.phys_objs[id - 1]) {
  3899. ret = i915_gem_init_phys_object(dev, id,
  3900. obj->base.size, align);
  3901. if (ret) {
  3902. DRM_ERROR("failed to init phys object %d size: %zu\n",
  3903. id, obj->base.size);
  3904. return ret;
  3905. }
  3906. }
  3907. /* bind to the object */
  3908. obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
  3909. obj->phys_obj->cur_obj = obj;
  3910. page_count = obj->base.size / PAGE_SIZE;
  3911. for (i = 0; i < page_count; i++) {
  3912. struct page *page;
  3913. char *dst, *src;
  3914. page = shmem_read_mapping_page(mapping, i);
  3915. if (IS_ERR(page))
  3916. return PTR_ERR(page);
  3917. src = kmap_atomic(page);
  3918. dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
  3919. memcpy(dst, src, PAGE_SIZE);
  3920. kunmap_atomic(src);
  3921. mark_page_accessed(page);
  3922. page_cache_release(page);
  3923. }
  3924. return 0;
  3925. }
  3926. static int
  3927. i915_gem_phys_pwrite(struct drm_device *dev,
  3928. struct drm_i915_gem_object *obj,
  3929. struct drm_i915_gem_pwrite *args,
  3930. struct drm_file *file_priv)
  3931. {
  3932. void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
  3933. char __user *user_data = to_user_ptr(args->data_ptr);
  3934. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  3935. unsigned long unwritten;
  3936. /* The physical object once assigned is fixed for the lifetime
  3937. * of the obj, so we can safely drop the lock and continue
  3938. * to access vaddr.
  3939. */
  3940. mutex_unlock(&dev->struct_mutex);
  3941. unwritten = copy_from_user(vaddr, user_data, args->size);
  3942. mutex_lock(&dev->struct_mutex);
  3943. if (unwritten)
  3944. return -EFAULT;
  3945. }
  3946. i915_gem_chipset_flush(dev);
  3947. return 0;
  3948. }
  3949. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  3950. {
  3951. struct drm_i915_file_private *file_priv = file->driver_priv;
  3952. /* Clean up our request list when the client is going away, so that
  3953. * later retire_requests won't dereference our soon-to-be-gone
  3954. * file_priv.
  3955. */
  3956. spin_lock(&file_priv->mm.lock);
  3957. while (!list_empty(&file_priv->mm.request_list)) {
  3958. struct drm_i915_gem_request *request;
  3959. request = list_first_entry(&file_priv->mm.request_list,
  3960. struct drm_i915_gem_request,
  3961. client_list);
  3962. list_del(&request->client_list);
  3963. request->file_priv = NULL;
  3964. }
  3965. spin_unlock(&file_priv->mm.lock);
  3966. }
  3967. static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  3968. {
  3969. if (!mutex_is_locked(mutex))
  3970. return false;
  3971. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  3972. return mutex->owner == task;
  3973. #else
  3974. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  3975. return false;
  3976. #endif
  3977. }
  3978. static unsigned long
  3979. i915_gem_inactive_count(struct shrinker *shrinker, struct shrink_control *sc)
  3980. {
  3981. struct drm_i915_private *dev_priv =
  3982. container_of(shrinker,
  3983. struct drm_i915_private,
  3984. mm.inactive_shrinker);
  3985. struct drm_device *dev = dev_priv->dev;
  3986. struct drm_i915_gem_object *obj;
  3987. bool unlock = true;
  3988. unsigned long count;
  3989. if (!mutex_trylock(&dev->struct_mutex)) {
  3990. if (!mutex_is_locked_by(&dev->struct_mutex, current))
  3991. return 0;
  3992. if (dev_priv->mm.shrinker_no_lock_stealing)
  3993. return 0;
  3994. unlock = false;
  3995. }
  3996. count = 0;
  3997. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
  3998. if (obj->pages_pin_count == 0)
  3999. count += obj->base.size >> PAGE_SHIFT;
  4000. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  4001. if (obj->active)
  4002. continue;
  4003. if (obj->pin_count == 0 && obj->pages_pin_count == 0)
  4004. count += obj->base.size >> PAGE_SHIFT;
  4005. }
  4006. if (unlock)
  4007. mutex_unlock(&dev->struct_mutex);
  4008. return count;
  4009. }
  4010. /* All the new VM stuff */
  4011. unsigned long i915_gem_obj_offset(struct drm_i915_gem_object *o,
  4012. struct i915_address_space *vm)
  4013. {
  4014. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4015. struct i915_vma *vma;
  4016. if (vm == &dev_priv->mm.aliasing_ppgtt->base)
  4017. vm = &dev_priv->gtt.base;
  4018. BUG_ON(list_empty(&o->vma_list));
  4019. list_for_each_entry(vma, &o->vma_list, vma_link) {
  4020. if (vma->vm == vm)
  4021. return vma->node.start;
  4022. }
  4023. return -1;
  4024. }
  4025. bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
  4026. struct i915_address_space *vm)
  4027. {
  4028. struct i915_vma *vma;
  4029. list_for_each_entry(vma, &o->vma_list, vma_link)
  4030. if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
  4031. return true;
  4032. return false;
  4033. }
  4034. bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
  4035. {
  4036. struct i915_vma *vma;
  4037. list_for_each_entry(vma, &o->vma_list, vma_link)
  4038. if (drm_mm_node_allocated(&vma->node))
  4039. return true;
  4040. return false;
  4041. }
  4042. unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
  4043. struct i915_address_space *vm)
  4044. {
  4045. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4046. struct i915_vma *vma;
  4047. if (vm == &dev_priv->mm.aliasing_ppgtt->base)
  4048. vm = &dev_priv->gtt.base;
  4049. BUG_ON(list_empty(&o->vma_list));
  4050. list_for_each_entry(vma, &o->vma_list, vma_link)
  4051. if (vma->vm == vm)
  4052. return vma->node.size;
  4053. return 0;
  4054. }
  4055. static unsigned long
  4056. i915_gem_inactive_scan(struct shrinker *shrinker, struct shrink_control *sc)
  4057. {
  4058. struct drm_i915_private *dev_priv =
  4059. container_of(shrinker,
  4060. struct drm_i915_private,
  4061. mm.inactive_shrinker);
  4062. struct drm_device *dev = dev_priv->dev;
  4063. int nr_to_scan = sc->nr_to_scan;
  4064. unsigned long freed;
  4065. bool unlock = true;
  4066. if (!mutex_trylock(&dev->struct_mutex)) {
  4067. if (!mutex_is_locked_by(&dev->struct_mutex, current))
  4068. return SHRINK_STOP;
  4069. if (dev_priv->mm.shrinker_no_lock_stealing)
  4070. return SHRINK_STOP;
  4071. unlock = false;
  4072. }
  4073. freed = i915_gem_purge(dev_priv, nr_to_scan);
  4074. if (freed < nr_to_scan)
  4075. freed += __i915_gem_shrink(dev_priv, nr_to_scan,
  4076. false);
  4077. if (freed < nr_to_scan)
  4078. freed += i915_gem_shrink_all(dev_priv);
  4079. if (unlock)
  4080. mutex_unlock(&dev->struct_mutex);
  4081. return freed;
  4082. }
  4083. struct i915_vma *i915_gem_obj_to_ggtt(struct drm_i915_gem_object *obj)
  4084. {
  4085. struct i915_vma *vma;
  4086. if (WARN_ON(list_empty(&obj->vma_list)))
  4087. return NULL;
  4088. vma = list_first_entry(&obj->vma_list, typeof(*vma), vma_link);
  4089. if (WARN_ON(vma->vm != obj_to_ggtt(obj)))
  4090. return NULL;
  4091. return vma;
  4092. }